vba for loop not counting rows correctly

  Kiến thức lập trình

This has me really confused…how come the counter variable “cntr” starts out with a value of 11, but the first time in the loop it gets a value of “102”?

It should be “11” and I don’t understand what’s going on.

Here’s a screenshot of the code next to the debug.print window .

Here’s the code:

Public Sub show_NNN_expenses()
    Dim StartRow As Integer, LastRow As Integer, isCamRow As Integer, ii As Integer, cntr As Integer
    
    StartRow = 11
    LastRow = 200
    ii = 0
    cntr = 11
Debug.Print ("Starting")
    'Loop through the rows
    For ii = StartRow To LastRow
Debug.Print ("StartRow " & StartRow & " Last Row: " & LastRow & " Current Row " & ii & " Cntr: " & cntr)
        'hide rows not "NNN" or "CAM"
        isCamRow = InStr(1, Cells(cntr, 1).Value, "CAM")
Debug.Print ("isCamRow " & isCamRow)

        If isCamRow > 0 Then
            'show row
           ' Cells(ii, 1).EntireRow.Hidden = False
        Else
            'hide row
           ' Cells(ii, 1).EntireRow.Hidden = True
            
        End If
        
        cntr = cntr + 1
    Next ii
        
End Sub

enter code here

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT