Exit statement
Exit the Do...Loop, For...Next, Function, or Sub code block.
Exit Do
Exit For
Exit Function
Exit Property
Exit Sub
ExitThe syntax of a statement can be found in the following forms:
Statement | describe |
---|---|
Exit Do | Provide an exitDo...LoopMethod of statement. Only inDo...LoopUsed in statements.Exit DoTransfer control toLoopThe statement after the statement. In nestedDo...LoopWhen used in a statement,Exit DoTransfer control to the previous nested loop where the loop is located. |
Exit For | Provide an exitForThe method of looping. Only inFor...NextorFor Each...NextUsed in a loop.Exit ForTransfer control toNextThe following statement. In nestedForWhen used in a loop,Exit ForTransfer control to the previous nested loop where the loop is located. |
Exit Function | Exit immediately from the location that appearsFunctionprocess. Continue to execute the callFunctionstatement after the statement. |
Exit Property | From where you arePropertyExit during the process. Continue to execute the following callPropertyThe statement of the process. |
Exit Sub | Exit immediately from the location that appearsSubProcess, continue to execute the callSubstatement after the statement. |
The following example shows how to use itExitStatement:
Sub RandomLoop Dim I, MyNum Do '
Set a dead loop。For I = 1 To 1000 '
cycle1000
Second-rate。MyNum = Int(Rnd * 100) '
Generate random numbers。Select Case MyNum '
Find the value of a random number。Case 17: MsgBox "Case 17"
Exit For '
in the case of17
,quitFor...Next
。Case 29: MsgBox "Case 29"
Exit Do '
in the case of29
,quitDo...Loop
。Case 54: MsgBox "Case 54"
Exit Sub '
in the case of54
,quitSub
process。End Select
Next
Loop
End Sub