Exit statement
Quit do ... Loop, for ... Next, Function, or Sub code block.
Exit Do
Exit For
Exit Function
Exit Property
Exit Sub
The syntax for the Exit statement is in the following ways:
Statement |
Description |
Exit do |
Provides an exit from do ... the method of the Loop statement. Only in Do ... used in the Loop statement. Exit Do transfers control to the statement after the Loop statement. In the nested do ... when used in the loop statement, theExit do transfers control to the upper-level nested loop where the loop is located. |
Exit for |
Provides a way to exit the For loop. Only for ... Next or for each ... Next Loop. Exit for transfers control to the statement after Next . When used in a nested for loop,Exit for transfers control to the upper-level nested loop where the loop is located. |
Exit Function |
Exit the Function procedure immediately from where it appears. Continue executing the statement following the statement that invokes the Function . |
Exit Property |
Exit immediately from the property process in which it is located. Continue with the following statement that calls the property procedure. |
Exit Sub |
Immediately exits the sub procedure from where it appears, and continues executing the statement following the statement that calls the sub . |
The following example illustrates how to use the Exit statement:
Sub randomloop Dim I, MyNum do '
sets the Dead loop. for I = 1 to 1000 '
loop 1000
times. MyNum = Int (Rnd *) '
produces random numbers. Select case MyNum '
to find the value of a random number. Case 17:msgbox "case"
exit for '
if is , exit
for ... Next . Case 29:msgbox "case"
exit do '
if
, exit do ... Loop
. Case 54:msgbox "case"
exit Sub '
if it is
, exit the Sub
procedure. End Select
Next
Loop
End Sub