| Dim Count:Count = 0 '定义一个变量,并赋值为0 For i = 1 To 10 '循环10次 Count = Count + 1 If Count = 5 Then '当变量Count的值为5时,退出当前循环 Exit For End If Next MsgBox Count '输出5 |
Do 循环 当(或直到)条件为True时循环
| 重复执行语句直到条件变为True Dim Count:Count = 5 '定义一个变量 Do Until Count = 0 '直到Count变量为0时,否则一直循环 MsgBox Count Count = Count -1 Loop MsgBox "循环结束" Dim Count:Count = 5 '定义一个变量 Do MsgBox Count Count = Count -1 Loop Until Count = 0 '直到Count变量为0时,否则一直循环 MsgBox "循环结束" 当条件变为True之前重复执行某语句块 Dim Count:Count = 5 '定义一个变量 Do While Count <> 0 '当Count变量为0时,停止循环 MsgBox Count Count = Count -1 Loop MsgBox "循环结束" Dim Count:Count = 5 '定义一个变量 Do MsgBox Count Count = Count -1 Loop While Count <> 0 '当Count变量为0时,停止循环 MsgBox "循环结束" Exit Do 语句用于退出 Do...Loop 循环 Dim Count:Count = 5 '定义一个变量 Do While Count <> 0 '当Count变量为0时,停止循环 MsgBox Count Count = Count -1 If Count = 2 Then '判断Count变量值是否为2,如果是则退出循环 Exit Do End If Loop MsgBox "循环结束" |
While…Wend 当条件为True时循环
| Dim Count:Count = 5 '定义一个变量 While Count <> 0 '当Count变量值不等于0,一直循环 MsgBox Count Count = Count -1 Wend MsgBox "循环结束" While…Wend 没有Exit语句,从头一直循环到尾,若要在中途退出,则需用Do…Loop语句 |
For Each...Next 语句
For Each...Next 不是将语句运行指定的次数,而是对于数组中的每个元素或对象集合中的每一项重复一组语句。这在不知道集合中元素的数目时非常有用。
| Dim Dics '定义一个变量 Set Dics = CreateObject("Scripting.Dictionary") '定义一个Dictionary对象 Dics.Add "0", "Athens" '为Dictionary对象赋值 Dics.Add "1", "Belgrade" Dics.Add "2", "Cairo" For Each Dic in Dics MsgBox Dics.Item(Dic) '循环遍历,且输出Dictionary键值 Next |







