+ Reply to Thread
Results 1 to 3 of 3

Loop is skipping rows

Hybrid View

  1. #1
    Registered User
    Join Date
    11-22-2011
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel 2003
    Posts
    3

    Loop is skipping rows

    Hi Everyone, good evening.

    I created a sweet macro for copying the information from a range of cells from a sheet (origin) into another sheet (destination) in the same workbook based on 2 criteria (sTLname and sCtriSt1,2,3 & 4).

    Both criteria are located in the Destination sheet. The sTLname is a String and the sCritSt are checkboxes (actually they are cells linked to 4 different checkboxes).

    The macro seemed to be working fine, but then I realised it was skipping some random rows.

    Please have a look at the code and let me know wth I am missing.

    Thank you all in advance.

    Sub GetList()
    
    Application.ScreenUpdating = False
    
    Dim lRowOrig As Long, lRowDest As Long, lLastRow As Long
    Dim sTLname As String, sCritSt1 As String, sCritSt2 As String, sCritSt3 As String, sCritSt4 As String
    Dim rOrig As Range, rDest As Range, rCritTl As Range, rCritSt As Range, rCurrList As Range
    
        lRowDest = 2
        lLastRow = Sheets("Origin").Range("D6").End(xlDown).Row
        sTLname = UCase(Sheets("Destination").Cells(1, 14).Value)
        sCritSt1 = Sheets("Destination").Cells(2, 14).Value
        sCritSt2 = Sheets("Destination").Cells(3, 14).Value
        sCritSt3 = Sheets("Destination").Cells(4, 14).Value
        sCritSt4 = Sheets("Destination").Cells(5, 14).Value
        Set rCurrList = Sheets("Destination").Range("B2").CurrentRegion
    
        rCurrList.Offset(1, 0).ClearContents
            
    
        For lRowOrig = 6 To lLastRow
            
        Set rCritTl = Sheets("Origin").Range("E" & lRowOrig)
        Set rCritSt = Sheets("Origin").Range("F" & lRowOrig)
        If rCritTl.Value = sTLname And (rCritSt.Value = sCritSt1 Or rCritSt.Value = sCritSt2 Or  rCritSt.Value = sCritSt3 Or rCritSt.Value = sCritSt4) Then
                      
        Set rOrig = Sheets("Origin").Range("D" & lRowOrig & ":H" & lRowOrig)
        Set rDest = Sheets("Destination").Range("B" & lRowDest & ":F" & lRowDest)
    
        rDest.Value = rOrig.Value
        lRowOrig = lRowOrig + 1
        lRowDest = lRowDest + 1
        
        End If
        Next lRowOrig
            
        Sheets("Destination").Cells(1, 14).Select
        Application.ScreenUpdating = True
        
        End Sub

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Loop is skipping rows

    At a glance, changing the for/next loop index variable inside a loop is a cardinal sin of programming:

        For lRowOrig = 6 To lLastRow
                '...
                lRowOrig = lRowOrig + 1
                '...
        Next lRowOrig
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    11-22-2011
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel 2003
    Posts
    3

    Talking Re: Loop is skipping rows

    Bloody sinner I am.

    It works beautifully now.

    Thank you very much!

    :D

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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