当前位置: 首页 > 文档资料 > VBA 中文教程 >

Exit For statement

优质
小牛编辑
128浏览
2023-12-01

当我们想要根据某些条件退出For循环时,使用Exit For语句。 执行Exit For ,控件会在For循环后立即跳转到下一个语句。

语法 (Syntax)

以下是VBA中Exit For Statement的语法。

 Exit For

流程图 (Flow Diagram)

VBA退出For声明

例子 (Example)

以下示例使用Exit For 。 如果Counter的值达到4,则退出For循环,控制在For循环后立即跳转到下一个语句。

Private Sub Constant_demo_Click()
   Dim a As Integer
   a = 10
   For i = 0 To a Step 2 'i is the counter variable and it is incremented by 2
      MsgBox ("The value is i is : " & i)
      If i = 4 Then
         i = i * 10 'This is executed only if i=4
         MsgBox ("The value is i is : " & i)
         Exit For 'Exited when i=4
      End If
   Next
End Sub

执行上述代码时,它会在消息框中输出以下输出。

The value is i is : 0
The value is i is : 2
The value is i is : 4
The value is i is : 40