We can exit any Do loop by using the Exit Do statement.
Do While Loop means to do something while the condition is TRUE. It is like a logical function which works based on TRUE or FALSE. So if the condition is TRUE, it will keep executing the statement inside the loop, but if the condition is FALSE straight away, it will exit the Do While statement.
VBA Do while is a loop in which you need to specify a condition and that condition must remain true for the loop to run. In simple words, first, it checks that the condition you have specified is true or not and if that condition is true it runs the loop, otherwise nothing.
The Exit While statement can provide another way to exit a While loop. Exit While immediately transfers control to the statement that follows the End While statement. You typically use Exit While after some condition is evaluated (for example, in an If...Then... Else structure).
While
constructs are terminated not with an End While
but with a Wend
.
While counter < 20
counter = counter + 1
Wend
Note that this information is readily available in the documentation; just press F1. The page you link to deals with Visual Basic .NET, not VBA. While (no pun intended) there is some degree of overlap in syntax between VBA and VB.NET, one can't just assume that the documentation for the one can be applied directly to the other.
Also in the VBA help file:
Tip The
Do...Loop
statement provides a more structured and flexible way to perform looping.
VBA is not VB/VB.NET
The correct reference to use is Do..Loop Statement (VBA). Also see the article Excel VBA For, Do While, and Do Until. One way to write this is:
Do While counter < 20
counter = counter + 1
Loop
(But a For..Next might be more appropriate here.)
Happy coding.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With