在 VBS/VBA 中退出 while 循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1271949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exit a while loop in VBS/VBA
提问by Helen
Is there a method of exiting/breaking a whilein VBS/VBA?
有没有一种while在 VBS/VBA中退出/打破 a 的方法?
Following code won't work as intended:
以下代码将无法按预期工作:
num = 0
while (num < 10)
if (status = "Fail") then
exit while
end if
num = num+1
wend
回答by Helen
VBScript's Whileloops don't support early exit. Use the Doloop for that:
VBScript 的While循环不支持提前退出。Do为此使用循环:
num = 0
do while (num < 10)
if (status = "Fail") then exit do
num = num + 1
loop
回答by rahul
what about changing the while loop to a do while loop
将 while 循环更改为 do while 循环怎么样
and exit using
并退出使用
Exit Do
回答by JunzCode
While Loop is an obsolete structure, I would recommend you to replace "While loop" to "Do While..loop", and you will able to use Exit clause.
虽然 Loop 是一个过时的结构,但我建议您将“While loop”替换为“Do While..loop”,这样您就可以使用 Exit 子句了。
check = 0
Do while not rs.EOF
if rs("reg_code") = rcode then
check = 1
Response.Write ("Found")
Exit do
else
rs.MoveNext
end if
Loop
if check = 0 then
Response.Write "Not Found"
end if}
回答by cogumel0
Incredibly old question, but bearing in mind that the OP said he does not want to use Do Whileand that none of the other solutions really work... Here's something that does exactlythe same as a Exit Loop:
令人难以置信的老问题,但请记住,OP 说他不想使用,Do While并且其他解决方案都没有真正起作用......这里有一些与 a完全相同的东西Exit Loop:
This never runs anything if the status is already at "Fail"...
如果状态已经是“失败”,这永远不会运行任何东西......
While (i < 20 And Not bShouldStop)
If (Status = "Fail") Then
bShouldStop = True
Else
i = i + 1
'
' Do Something
'
End If
Wend
Whereas this one always processes something first (and increment the loop variable) before deciding whether it should loop once more or not.
而这个总是先处理一些东西(并增加循环变量),然后再决定是否应该再次循环。
While (i < 20 And Not bShouldStop)
i = i + 1
'
' Do Something
'
If (Status = "Fail") Then
bShouldStop = True
End If
Wend
Ultimately, if the variable Statusis being modified inside the While(and assuming you don't need ioutside the while, it makes no difference really, but just wanted to present multiple options...
最终,如果变量Status在While( 并且假设您不需要i在 while 之外进行修改,那么实际上并没有什么区别,但只是想提供多个选项......
回答by codemaster
Use Do...Loop with Until keyword
使用 Do...Loop 和直到关键字
num=0
Do Until //certain_condition_to_break_loop
num=num+1
Loop
This loop will continue to execute, Untilthe condition becomes true
这个循环会继续执行,直到条件成立
While...Wend is the old syntax and does not provide feature to break loop! Prefer do while loops
While...Wend 是旧语法,不提供中断循环的功能!更喜欢使用 while 循环
回答by JoshHetland
I know this is old as dirt but it ranked pretty high in google.
我知道这和污垢一样古老,但它在谷歌中的排名相当高。
The problem with the solution maddy implemented (in response to rahul) to maintain the use of a While...Wend loop has some drawbacks
maddy 为维持使用 While...Wend 循环而实施的解决方案(响应 rahul)的问题有一些缺点
In the example given
在给出的例子中
num = 0
While num < 10
If status = "Fail" Then
num = 10
End If
num = num + 1
Wend
After status = "Fail" num will actually equal 11. The loop didn't end on the fail condition, it ends on the next test. All of the code after the check still processed and your counter is not what you might have expected it to be.
在 status = "Fail" num 实际上等于 11 之后。循环不会在失败条件下结束,而是在下一个测试时结束。检查后的所有代码仍在处理,您的计数器不是您所期望的那样。
Now depending on what you are all doing in your loop it may not matter, but then again if your code looked something more like:
现在取决于您在循环中所做的一切,这可能无关紧要,但是如果您的代码看起来更像:
num = 0
While num < 10
If folder = "System32" Then
num = 10
End If
RecursiveDeleteFunction folder
num = num + 1
Wend
Using Do Whileor Do Untilallows you to stop execution of the loop using Exit Doinstead of using trickery with your loop condition to maintain the While ... Wendsyntax. I would recommend using that instead.
使用Do WhileorDo Until允许您停止循环的执行 usingExit Do而不是使用带有循环条件的技巧来维护While ... Wend语法。我建议改用它。

