- VBA 教程
- VBA - 主頁
- VBA - 概覽
- VBA - Excel 宏
- VBA - Excel 術語
- VBA - 宏註釋
- VBA - 訊息框
- VBA - 輸入框
- VBA - 變數
- VBA - 常量
- VBA - 運算子
- VBA - 判定
- VBA - 迴圈
- VBA - 字串
- VBA - 日期和時間
- VBA - 陣列
- VBA - 函式
- VBA - 子過程
- VBA - 事件
- VBA - 錯誤處理
- VBA - Excel 物件
- VBA - 文字檔案
- VBA - 程式設計圖表
- VBA - 使用者窗體
- 對你有用的 VBA 資源
- VBA - 快速指南
- 對你有用的 VBA 資源
- VBA - 討論
VBA - 退出 For
在明確某項準則條件下,我們想退出 For 迴圈時,會用到 Exit For 語句。執行 Exit For 時,控制權會立即跳至 For 迴圈後的下一個語句。
語法
以下為 VBA 中 Exit For 語句的語法。
Exit For
流程圖
示例
以下示例使用了 Exit For。如果計數器的值達到 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
vba_loops.htm
廣告
