VBA退出for循環
Exit For 語句使用在需要依據一定的標準來退出For循環。當Exit For 被執行,控製係統就會馬上跳到 For 循環後,下一條語句。
語法:
VBA Exit For 語句的語法:
Exit For
流程圖
示例 :
下麵的示例使用Exit For。如果計數器的值達到4,對於Exit 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