Exit Statement

The Exit statement causes control to continue at the statement following the end of a loop without the loop conditions being satisfied.

# Notes

# Syntax

Exit

In contrast, the Continue statement in a loop continues execution with the next iteration of the loop while skipping over the statements between Continue and the end of the loop.

If you use Exit to exit a function, the Function will return the default value for the data type of the returned variable.

# Example

This example increments a counter variable. If the user presses Esc on Windows or Linux or Command-Period on Macintosh, the UserCancelled function will return True and the loop will exit. Otherwise, the loop will continue until the Stop variable is True:

Dim counter as Integer
Do
counter=counter+1
If UserCancelled then
Exit
End if
Loop Until Stop

If you use the DoEvents method of the Application class inside a loop such as this, then the UserCancelled function does not detect the Esc key or the Command-period sequence. When you use DoEvents, the user interface remains responsive while the loop is executing, so you can add a button that the user can click to stop the loop.

# See Also

Continue,Do...Loop, For...Next, Goto,While...Wend statements.