Decrement in for each loop

2019-07-12 02:48发布

Is it possible to decrement in For each loop in VBA for Excel?

I have code like this:

Sub Makro1()

Dim rng As Range
Dim row As Range
Dim cell As Range

Set rng = Range("B1:F18")

For Each row In rng.Rows
    If WorksheetFunction.CountA(row) = 0 Then
        row.EntireRow.Delete
        'Previous row
    End If
Next row

End Sub

And I want to step back in commented statement. Is it possible?

1条回答
甜甜的少女心
2楼-- · 2019-07-12 03:21

No.

You have to use a For...Next loop and step backwards:

Dim i As Long
For i = rng.Rows.Count To 1 Step -1
    Set row = rng.Rows(i)
    If WorksheetFunction.CountA(row) = 0 Then
        row.EntireRow.Delete
        'Previous row
    End If
Next i
查看更多
登录 后发表回答