3

I have a quick question about which I did not find specific information on the web. I want to perform a Do...Loop Until loop, but I would like to insert more than one condition at the end. I would like to do:

Do
    ' ...my code...
Loop Until [Condition 1] And [Condition 2] And....And [Condition n]`

Is this possible?

Thank you very much in advance, Orlando

2
  • 7
    Can you not? Have you tried that? Do you get an error?
    – BruceWayne
    Aug 11, 2016 at 15:36
  • 2
    I did not find specific information on the web ...uh, I'm pretty sure Do [statements] Loop Until [**Boolean Expression**] is documented somewhere Aug 11, 2016 at 16:17

3 Answers 3

7

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.

1
  • Thank you very much, especially for the tip to speed up the calculations! :)
    – OrlandoT
    Aug 12, 2016 at 13:18
6

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
6

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.