减少每个循环

时间:2014-09-10 13:02:54

标签: excel-vba foreach vba excel

是否可以减少VBA for Excel中的每个循环?

我有这样的代码:

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

我想退回评论声明。有可能吗?

1 个答案:

答案 0 :(得分:11)

没有

您必须使用For...Next循环并向后退步:

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