czarek zmuda
czarek zmuda

Reputation: 43

Decrement in for each loop

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?

Upvotes: 2

Views: 18531

Answers (1)

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

Upvotes: 13

Related Questions