vba 做...循环直到有多个条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38900644/
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
Do... Loop Until with multiple conditions
提问by OrlandoT
I have a quick question about which I did not find specific information on the web.
I want to perform a Do...Loop Untilloop, but I would like to insert more than one condition at the end. I would like to do:
我有一个简单的问题,我在网上没有找到具体信息。我想执行一个Do...Loop Until循环,但我想在最后插入多个条件。我想要做:
Do
' ...my code...
Loop Until [Condition 1] And [Condition 2] And....And [Condition n]`
Is this possible?
这可能吗?
Thank you very much in advance, Orlando
非常感谢您,奥兰多
回答by jsanchezs
It is possible indeed as if you were using "if " statement, for example:
确实可以像您使用“if”语句一样,例如:
Do Until rngCell.Value="" Or rngCell.Value="abc"
DatePresent = (rngCell.Value = "RESP") Or (rngCell.Value ="Respiratory")
Set rngCell = rngCell.Offset(1)
Loop
回答by Lew
You can use Andand/or Or:
您可以使用And和/或Or:
Do
'Your code
Loop until condition1 And condition2
Andwill continue all the time all conditions are met.
Orwill continue when one or more of the conditions are met.
And将一直持续到所有条件都满足。
Or当满足一个或多个条件时将继续。
You can have any number of conditions.
您可以有任意数量的条件。
回答by omegastripes
The below example shows lazy evaluation implementation:
下面的例子展示了惰性求值的实现:
Do
' some operations
Select Case False
Case Condition1
Case Condition2
Case Condition3
Case ConditionN
Case Else Exit Do
End Select
Loop
Such code allows to improve performance and speed up code execution. It evaluates conditions one by one until first false result only, if all conditions are true then it exits the loop, while conventional Andoperators evaluate all conditions regardless of the results.
这样的代码可以提高性能并加速代码执行。它一个接一个地评估条件,直到只有第一个错误结果,如果所有条件都为真,那么它退出循环,而传统And运算符评估所有条件而不管结果。

