- 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 - 針對每個迴圈
針對每個迴圈用於執行一系列或一組語句以針對陣列或集合中的每個元素。
針對每個迴圈類似於針對迴圈;但針對每個元素或組執行該迴圈。因此,此類迴圈中不會有步驟計數器。此方法最常用於針對陣列或檔案系統物件上下文,以便遞迴執行。
語法
以下是 VBA 中針對每個迴圈的語法。
For Each element In Group [statement 1] [statement 2] .... [statement n] [Exit For] [statement 11] [statement 22] Next
示例
Private Sub Constant_demo_Click()
'fruits is an array
fruits = Array("apple", "orange", "cherries")
Dim fruitnames As Variant
'iterating using For each loop.
For Each Item In fruits
fruitnames = fruitnames & Item & Chr(10)
Next
MsgBox fruitnames
End Sub
執行上述程式碼時,將在每行列印所有水果名稱。
apple orange cherries
vba_loops.htm
廣告
