To break out of a while loop, you can use the endloop, continue, resume, or return statement.
We can exit any Do loop by using the Exit Do statement.
This example uses the While...Wend statement to increment a counter variable. The statements in the loop are executed as long as the condition evaluates to True. Dim Counter Counter = 0 ' Initialize variable. While Counter < 20 ' Test value of Counter.
Advertisements. In a While…Wend loop, if the condition is True, all the statements are executed until the Wend keyword is encountered. If the condition is false, the loop is exited and the control jumps to the very next statement after the Wend keyword.
A While
/Wend
loop can only be exited prematurely with a GOTO
or by exiting from an outer block (Exit sub
/function
or another exitable loop)
Change to a Do
loop instead:
Do While True
count = count + 1
If count = 10 Then
Exit Do
End If
Loop
Or for looping a set number of times:
for count = 1 to 10
msgbox count
next
(Exit For
can be used above to exit prematurely)
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