Excel VBA--退出for循环

我想在满足内部条件时退出我的for循环。当 "if "条件得到满足时,我怎样才能退出我的 "for "循环?我想在 "if "语句的末尾有某种退出方式,但不知道该如何操作。

Dim i As Long
For i = 1 To 50
    Range("B" & i).Select
    If Range("B" & i).Value = "Artikel" Then
        Dim temp As Long
        temp = i
    End If
Next i
Range("A1:Z" & temp - 1).EntireRow.Delete Shift:=xlToLeft
解决办法

要提前退出你的循环,你可以使用 "Exit For"。

如果[条件],则退出For"。

评论(1)

另一种提前退出For循环的方法是通过改变循环计数器。

For i = 1 To 10
    If i = 5 Then i = 10
Next i

Debug.Print i   '11

For i = 1 To 10
    If i = 5 Then Exit For
Next i

Debug.Print i   '5
评论(2)