Exit statement
Exit the Do... Loop, For... Next, Function, or Sub code block.
Exit Do
Exit For
Exit Function
Exit Property
Exit Sub
ExitThe statement syntax has the following forms:
| Statement |
Description |
| Exit Do |
Provides an exit method.Do... LoopStatement method. OnlyDo... LoopStatement.Exit DoTransfer controlLoopStatement after the statement. In the nestedDo... LoopWhen used in a statement,Exit DoTransfers control to the previous nested loop where the loop is located. |
| Exit |
Provides an exit method.ForLoop Method. OnlyFor... NextOrFor Each... NextLoop.ExitTransfer controlNext. In the nestedForWhen used in a loop,ExitTransfers control to the previous nested loop where the loop is located. |
| Exit Function |
Exit immediatelyFunctionProcess. Continue callingFunction. |
| Exit Property |
FromPropertyExit during the process. Continue to execute the following callPropertyStatement of the process. |
| Exit Sub |
Exit immediatelySubProcess, continue to execute the callSub. |
The following example illustrates how to useExitStatement:
Sub RandomLoop Dim I, MyNum Do ' Set an endless loop. For I = 1 To 1000 ' Loop 1000 Times. MyNum = Int(Rnd * 100) 'Generate a random number. Select Case MyNum 'Returns the value of a random number. Case 17: MsgBox "Case 17" Exit For ' If yes 17, Exit For...Next. Case 29: MsgBox "Case 29" Exit Do ' If yes 29, Exit Do...Loop. Case 54: MsgBox "Case 54" Exit Sub ' If yes 54, Exit Sub Process. End Select Next LoopEnd Sub