Excel VBA deleting rows in a for loop misses rows
Since there's no Reverse loop for For Each
you need to use a slightly different approach.
Also, your code with multiple If
s and OR
is "screaming for the use of Select Case
.
Dim StatusRange As Range
Dim i As Long
Set StatusRange = Range("B2", Range("B2").End(xlDown))
' loop backward when deleting Ranges, Rows, Cells
For i = StatusRange.Rows.Count To 1 Step -1
Select Case StatusRange(i, 1).Value
Case "FG", "QC", "CS"
StatusRange(i, 1).EntireRow.Delete
Case Else ' for the future if you need it
End Select
Next i
Work from the bottom up. If you delete a row, everything moves up and you skip that row on the next iteration.
Here is the 'guts' of the code to work up from the bottom.
With Worksheets("Sheet1")
For rw = .Cells(.Rows.Count, "B").End(xlUp).Row To 2 Step -1
Select Case UCase(.Cells(rw, "B").Value2)
Case "FG", "QC", "CS"
.Rows(rw).EntireRow.Delete
End Select
Next rw
End With