VBA - Exit Do 语句
当我们想要根据某些条件退出Do循环时,使用Exit Do语句。 它可以在 Do...While 和 Do...Until 循环中使用。
当执行Exit Do时,控制立即跳转到Do循环之后的下一条语句。
语法
以下是 VBA 中 Exit Do 语句的语法。
Exit Do
示例
以下示例使用Exit Do。 如果 Counter 的值达到 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

