-
StackOverflow 文档
-
VBA 教程
-
流程控制结构
-
Do 循环
Public Sub DoLoop()
Dim entry As String
entry = ""
'Equivalent to a While loop will ask for strings until "Stop" in given
'Prefer using a While loop instead of this form of Do loop
Do While entry <> "Stop"
entry = InputBox("Enter a string, Stop to end")
Debug.Print entry
Loop
'Equivalent to the above loop, but the condition is only checked AFTER the
'first iteration of the loop, so it will execute even at least once even
'if entry is equal to "Stop" before entering the loop (like in this case)
Do
entry = InputBox("Enter a string, Stop to end")
Debug.Print entry
Loop While entry <> "Stop"
'Equivalent to writing Do While Not entry="Stop"
'
'Because the Until is at the top of the loop, it will
'not execute because entry is still equal to "Stop"
'when evaluating the condition
Do Until entry = "Stop"
entry = InputBox("Enter a string, Stop to end")
Debug.Print entry
Loop
'Equivalent to writing Do ... Loop While Not i >= 100
Do
entry = InputBox("Enter a string, Stop to end")
Debug.Print entry
Loop Until entry = "Stop"
End Sub