SoFunction
Updated on 2025-04-09

VBS tutorial: VBscript statement-Exit statement

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          ' cycle 1000 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 of 17,quit For...Next        Case 29: MsgBox "Case 29"          Exit Do              ' in the case of 29,quit Do...Loop        Case 54: MsgBox "Case 54"          Exit Sub             ' in the case of 54,quit Sub process。        End Select    Next  LoopEnd Sub