Exit Statement |
Exit Do Exit For Exit Function Exit Property Exit Select Exit Sub Exit Try Exit While
Prematurely exits a block of code
Exits a Do...Loop statement. If the current Do...Loop is within a nested Do...Loop , execution continues with the next Loop statement wrapped around the current one. If, however, the Do...Loop is standalone, program execution continues with the first line of code after the Loop statement.
Exits a For...Next loop or a For Each...Next statement. If the current For...Next is within a nested For...Next loop, execution continues with the next Next statement wrapped around the current one. If, however, the For...Next loop is standalone, program execution continues with the first line of code after the Next statement.
Exits the current function. Program execution is passed to the line following the call to the function.
Exits the current property procedure. Program execution is passed to the line following the call to the property.
Immediately exits a Select Case construct. Execution continues with the statement immediately following the End Select statement.
Exits the current sub procedure. Program execution is passed to the line following the call to the procedure.
Immediately exits a Try...Catch block. Program execution proceeds with the Finally block, if it is present, or with the statement following the End Try statement.
Immediately exits a While loop. Program execution proceeds with the code following the End While statement. If Exit While is within a nested While loop, it terminates the loop at the level of nesting in which Exit While occurs.
Using Exit Sub can save having to wrap lengthy code within an If...Then statement. Here is an example with Exit Sub :
Sub MyTestSub(iNumber As Integer) If iNumber = 10 Then Exit Sub End If . . .'code End Sub
and without Exit Sub :
Sub MyTestSub(iNumber As Integer) If iNumber <> 10 Then . . . 'code End If End Sub
End... Statement