Do… Loop Until with multiple conditions

与世无争的帅哥 提交于 2019-12-01 21:50:37

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 And operators evaluate all conditions regardless of the results.

It is possible indeed as if you were using "if " statement, for example:

Do Until rngCell.Value="" Or rngCell.Value="abc"
   DatePresent = (rngCell.Value = "RESP") Or (rngCell.Value ="Respiratory")
   Set rngCell = rngCell.Offset(1)
Loop

You can use And and/or Or:

Do
  'Your code
Loop until condition1 And condition2

And will continue all the time all conditions are met. Or will continue when one or more of the conditions are met.

You can have any number of conditions.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!