专注Java教育14年 全国咨询/投诉热线:444-1124-454
赢咖4LOGO图
始于2009,口口相传的Java黄埔军校
首页 hot资讯 vba退出for循环

vba退出for循环

更新时间:2022-09-09 09:56:18 来源:赢咖4 浏览5307次

当我们要根据特定条件退出For循环时,使用Exit For语句。当执行Exit For时,控件会立即跳转到For循环之后的下一条语句。

句法

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

Exit For

流程图

例子

以下示例使用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 

 

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>