当想要根据特定标准退出Do循环时,可使用Exit Do语句。 它可以同时用于Do...While和Do...Until直到循环。
当Exit Do被执行时,控制器在Do循环之后立即跳转到下一个语句。
语法
以下是在VBA中Exit Do语句的语法。
Exit Do
示例
以下示例演示如何使用Exit Do语句,如果计数器的值达到10,则退出Do循环,并在For循环之后立即跳转到下一个语句。
Private Sub Constant_demo_Click()
i = 0
Do While i <= 100
If i > 10 Then
Exit Do ' Loop Exits if i>10
End If
MsgBox ("The Value of i is : " & i)
i = i + 2
Loop
End Sub
当上面的代码被执行时,它会在消息框中输出下面的输出。
The Value of i is : 0
The Value of i is : 2
The Value of i is : 4
The Value of i is : 6
The Value of i is : 8
The Value of i is : 10
