I want to loop over all rows in the active worksheet and compare the values of a column.
However, it skips every second value.
The worksheet looks like this:
Code:
Sub test()
Dim ws As Worksheet
Dim r As Range
Dim total As Integer
Dim value As String
Set ws = ActiveSheet
total = 0
For Each r In ws.UsedRange.rows
value = r.Cells(r.Row, 3).value
If value = "Test" Then
'Do something...
total = total + 1
End If
Debug.Print ("Row: " & r.Row & " - Value: " & value)
Next r
End Sub
The console shows this:
Row: 1 - Value: Col2
Row: 2 - Value: JohnDoe
Row: 3 - Value: JohnDoe
Row: 4 - Value: Test
Row: 5 - Value: Blabla
Row: 6 - Value: Blabla
Row: 7 - Value:
Row: 8 - Value:
Row: 9 - Value:
Row: 10 - Value:
Row: 11 - Value:
How do I fix this, so it does not skip every second value?
The reason I am using UsedRange
instead of for i = 1 to x
, is that I will not now the number of rows beforehand.