I have a subroutine that deletes rows in a range containing around 1000 rows.
Rows are deleted on a critera.
The code below works.
However, when I run the macro I usually have to run it 4 times before all rows containing the removal criteria are removed.
I guess this is because the for loop misses its index when a row suddenly dissapears when deleting a row.
My first code looks like this.
Set StatusRange = Range("B2", Range("B2").End(xlDown))
For Each StatusCell In StatusRange
If StatusCell = "FG" Then
StatusCell.EntireRow.Delete
ElseIf StatusCell = "QC" Then
StatusCell.EntireRow.Delete
ElseIf StatusCell = "CS" Then
StatusCell.EntireRow.Delete
Else
End If
Next StatusCell
When I try to update the range each loop, it still doesn't work.
Set StatusRange = Range("B2", Range("B2").End(xlDown))
For Each StatusCell In StatusRange
If StatusCell = "FG" Then
StatusCell.EntireRow.Delete
ElseIf StatusCell = "QC" Then
StatusCell.EntireRow.Delete
ElseIf StatusCell = "CS" Then
StatusCell.EntireRow.Delete
Else
End If
Set StatusRange = Range("B2", Range("B2").End(xlDown))
Next StatusCell
Is there anyone who know a sloution to this?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…