+ Reply to Thread
Results 1 to 3 of 3

Macro works for several loops and then slows down

Hybrid View

Ethanrholt Macro works for several loops... 12-14-2016, 11:58 AM
Marc L Re: Macro works for several... 12-14-2016, 12:12 PM
EssoExplJoe Re: Macro works for several... 12-14-2016, 12:16 PM
  1. #1
    Registered User
    Join Date
    09-06-2013
    Location
    Northamptonshire, England
    MS-Off Ver
    Excel 2003
    Posts
    45

    Macro works for several loops and then slows down

    Hi all,

    I am running the following macro in order to take rows from one sheet, and based on the contents of particular columns, pasted those rows into another sheet.

    Sub Import_New_1st_Line_Calls_User2()
    
    Sheets("1st Line Sample").Calculate
    
    
       Dim LSearchRow As Integer
       Dim LCopyToRow As Integer
    
    On Error GoTo Err_Execute
       'Start search in row
       LSearchRow = 11
    
       'Start copying data to next free row
       With Sheets("User 2")
       LCopyToRow = .Cells(.Rows.Count, "B").End(xlUp).Row + 1
       End With
    
       While Len(Range("A" & CStr(LSearchRow)).Value) > 0
    
          'If value in column AB = "2nd Line", copy entire row
          If Range("AG" & CStr(LSearchRow)).Value = "User 2" Then
    
             'Select row to copy
             Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
             Selection.Copy
    
             'Paste into Tracker
             Sheets("User 2").Select
             Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
             ActiveSheet.Paste
    
             'Move counter to next row
             LCopyToRow = LCopyToRow + 1
    
             Sheets("1st Line Sample").Select
    
          End If
    
          LSearchRow = LSearchRow + 1
    
       Wend
       
       Application.CutCopyMode = False
       Range("A3").Select
          
        Sheets("User 2").Select
        Columns("AF:AG").Select
        Selection.ClearContents
        Sheets("1st Line Sample").Select
       
       Exit Sub
    
    Err_Execute:
       MsgBox "An error occurred."
    
    End Sub

    The above seems to run for around 25 rows and then it slow down (taking around 30+ seconds) to process each row.

    Assuming that the sheets are formatted correctly and the relevant data is present, is there any reason why this may be occurring after a seemingly random amount of loops?

    Thank you.

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Macro works for several loops and then slows down


    Hi !

    This kind of stuff is well performed by an Excel basics : an advanced filter ‼
    In only one codeline, data are filtered and copied to destination !

    See inner VBA help of AdvancedFilter method or
    activate Macro recorder and operate manually, you will get your own base code !

    See also samples within threads of this forum …
    Last edited by Marc L; 12-14-2016 at 12:17 PM. Reason: typo …

  3. #3
    Forum Contributor
    Join Date
    07-23-2016
    Location
    Texas
    MS-Off Ver
    2016
    Posts
    274

    Re: Macro works for several loops and then slows down

    You can speed up the copy buy using ranges in vba instead of selecting. The selection takes time and is unnecessary.

+ 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] Code that loops through number of files and runs a called sub on each works sometimes. Why
    By jbwitty in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-01-2013, 05:34 PM
  2. Macro slows down as it runs
    By Dean Staples in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-30-2013, 09:16 PM
  3. [SOLVED] Macro slows down with each successive run
    By hillbk in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-07-2013, 11:45 PM
  4. Macro works and loops perfectly in VBA but does not loop on button click
    By evan.sams in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-25-2012, 08:25 PM
  5. Macro slows down with no visible reason
    By PoncjuszPilat in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-27-2012, 10:06 AM
  6. Problems with indicies in For loops, it works with integers but not the index.
    By msfarrar in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-22-2012, 10:46 AM
  7. Excel 2007 : Macro - loop slows down
    By IALTO in forum Excel General
    Replies: 17
    Last Post: 05-20-2010, 12:50 PM
  8. [SOLVED] macro slows down
    By Gary Keramidas in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-21-2006, 07:45 PM

Tags for this Thread

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