+ Reply to Thread
Results 1 to 9 of 9

Write a more efficient code

Hybrid View

  1. #1
    Registered User
    Join Date
    04-20-2015
    Location
    Norway
    MS-Off Ver
    360
    Posts
    24

    Write a more efficient code

    Hi all!

    I’ve written a (copy-paste based) code to transform data, and it’s quite slow for thousands of companies. However, I lack the intuition for a better approach to the problem. If someone can help me out or point me in the right direction, I would be very thankful

    An example is attached.

    A description of the problem follows:
    I get datasets that consist of company data, where I need to transform the format. Every company comes with:
    - 1 row with Name
    - X number of rows with fixed information
    - Y number of columns with (time) variable data
    - 1 column with dates.


    The date-column causes the headers to be off by one column. Furthermore, some companies have no data for the selected period, and should be deleted altogether.

    The transformed data has one row for every date, and one column for every other datatype (fixed and variable).
    Attached Files Attached Files

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Write a more efficient code

    Hi Karl,

    Not all of your code is included in the sample

    It looks as though you can do some block deletions:

    'Call XDeleteData1Rows(Range1End)
        Worksheets("Data input").Rows("1:" & Range1End).EntireRow.Delete
    Last edited by xladept; 04-25-2016 at 11:35 AM.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Registered User
    Join Date
    04-20-2015
    Location
    Norway
    MS-Off Ver
    360
    Posts
    24

    Re: Write a more efficient code

    You're right, I'm sorry..
    It's fixed now!
    Attached Files Attached Files

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Write a more efficient code

    Well, so far, just the block deletions for these two deletion routines:

    'Call XDeleteData1Rows(Range1End)
        Worksheets("Data input").Rows("1:" & Range1End).EntireRow.Delete
    AND

    'Call XDeleteData2Rows(LR)
        Worksheets("Data input").Rows("1:" & LR).EntireRow.Delete

  5. #5
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Write a more efficient code

    Add this copy routine code change:

    Sub XCopyData1(ByRef Times)
    
    Dim LC As Long
    Dim LR As Long
    Dim LR2 As Long
    
        LC = Sheets("Data output").Cells(1, Columns.Count).End(xlToLeft).Column
            
        LR = Worksheets("Data output").Range("A" & Rows.Count).End(xlUp).Row
            
        Worksheets("Data output").Range("A" & LR).Resize(Times, LC).Value = _
        Worksheets("Data output").Range("A" & LR).Resize(1, LC).Value
        
        
    End Sub
    *Sample took 3.4s at start now down to 1.2s
    Last edited by xladept; 04-25-2016 at 01:13 PM.

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Write a more efficient code

    I'm sorry too - I edited the last post with an observation. I'll be looking at your code off and on for maybe another hour

  7. #7
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Write a more efficient code

    The first thing I would do would be to delete all the blank data rows.

    I will create a code snippet for you to try.


    
    Sub Macro1()
    
    Optimise (True)
    
    LR = Cells(Rows.Count, 1).End(xlUp).Row
        Range("I1:I" & LR).FormulaR1C1 = _
            "=IF(CONCAT(RC[-8]:RC[-1])=REPT(""@NA"",COLUMNS(RC[-8]:RC[-1])),0,1)"
        Range("I1:I" & LR).Value = Range("I1:I" & LR).Value
        Range("J1:J" & LR).FormulaR1C1 = "=ROW()"
        Range("J1:J" & LR).Value = Range("J1:J" & LR).Value
    
        ActiveWorkbook.Worksheets("Data input").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Data input").Sort.SortFields.Add Key:=Range( _
            "I1:I" & LR), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
            xlSortNormal
        ActiveWorkbook.Worksheets("Data input").Sort.SortFields.Add Key:=Range( _
            "J1:J" & LR), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("Data input").Sort
            .SetRange Range("A1:J" & LR)
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        
     Set P = Columns("I:I").Find(What:="0", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Row
        Rows(P & ":" & LR).Delete Shift:=xlUp
        Columns("I:J").Delete Shift:=xlToLeft
        
    Optimise (False)
    
    End Sub
    
    Sub Optimise(Flag As Boolean)
    On Error Resume Next
    F = Not Flag
    Application.ScreenUpdating = F
    Application.DisplayAlerts = F
    Application.EnableEvents = F
    Application.DisplayStatusBar = F
    ActiveSheet.DisplayPageBreaks = F
    If F = True Then
    Application.Calculation = xlCalculationAutomatic
    Else
    Application.Calculation = xlCalculationManual
    End If
    Changeflag = 0
    On Error GoTo 0
    End Sub
    Last edited by mehmetcik; 04-25-2016 at 12:42 PM.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  8. #8
    Registered User
    Join Date
    04-20-2015
    Location
    Norway
    MS-Off Ver
    360
    Posts
    24

    Re: Write a more efficient code

    Thank you both for replying
    I went with your advice (again), xladept, and ended up with a much shorter and faster code!

    Thank you for your time

  9. #9
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Write a more efficient code

    You're welcome!

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] VBA Delete Row Code - Need more efficient code to speed it up
    By matcapir in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-02-2015, 04:32 PM
  2. Slow code- making code more efficient
    By ammartino44 in forum Excel General
    Replies: 4
    Last Post: 05-06-2015, 12:47 PM
  3. More efficient way to write this formula?
    By Destroy in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-12-2014, 10:31 AM
  4. [SOLVED] Trying to change verbose looping code into more efficient (and shorter) code please
    By dawatcher in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-03-2013, 07:43 AM
  5. Is there a more efficient way to write this formula
    By Excelsius in forum Excel General
    Replies: 8
    Last Post: 02-01-2012, 11:12 AM
  6. Most Efficient Way to Write Out Data
    By ld_pvl in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 01-09-2011, 04:01 PM
  7. Efficient Code
    By GregR in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-27-2005, 12:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1