I know I can get around my problem with a different approach, like looping through row index numbers instead of for/each row, but I'm still curious if the problem can be averted more directly.
Namely: I reference a range. I use a For/Each loop to go through MyRange.Rows. If a row meets a condition, I delete that row. Problem is if the next row also meets the condition--it gets skipped by the 'Next' part of the loop. So in a block of rows to delete, I only get every other one.
Here is a code example with an attempted and failed solution:
The two lines inside the "delete me" condition were just the single line "Row.Delete" in my first attempt. That approach only got every other row. This second attempt did the same thing--skipping rows--however! I can't fool the next loop into not skipping the row after the deleted row it seems--vba remembers!
Am I missing something simple, to use For/Next for this task? Thanks for looking.
Namely: I reference a range. I use a For/Each loop to go through MyRange.Rows. If a row meets a condition, I delete that row. Problem is if the next row also meets the condition--it gets skipped by the 'Next' part of the loop. So in a block of rows to delete, I only get every other one.
Here is a code example with an attempted and failed solution:
Code:
Dim MyRange as Range
Dim Row as range
For Each Row in MyRange.Rows
If Row.Cells(1, ColIndex) = "delete me" Then
Set Row = Row.Offset(-1, 0) 'This could blow up on the first row...
Row.Offset(1, 0).Delete
End If
Next Row
Am I missing something simple, to use For/Next for this task? Thanks for looking.