vba 每个循环递减

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25766166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 04:29:54  来源:igfitidea点击:

Decrement in for each loop

excel-vbaforeachvbaexcel

提问by czarek zmuda

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

是否可以在 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?

我想在评论声明中退一步。是否可以?

回答by Jean-Fran?ois Corbett

No.

不。

You have to use a For...Nextloop and step backwards:

您必须使用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