Exit Statement

Language Items List

Definition:

Provides an alternative exit for Do...Loops, For...Next loops, Function procedures, or Sub procedures.

Syntax:

Exit {Do | For | Function | Sub }

Details:

You can use the Exit statement in combination with various keywords to exit several types of statement blocks, as shown in the following table. The type of exit used must match the type of statement block in which it is being used:


Statement Description


Exit Do Exits a Do...Loop statement and transfers control to the statement following the Loop statement. It can be used only inside a Do...Loop statement. If Exit Do is used within nested Do..Loop statements, it transfers control to the loop that is one nested level above the loop in which it occurs.

Exit For Exits a For...Next loop and transfers control to the statement following the Next statement. It can only be used in a For...Next loop. If Exit For is used within nested For...Next loops, it transfers control to the loop that is one nested level above the loop in which it occurs.

Exit Function Immediately exits the Function procedure in which it appears. Program execution proceeds with the statement following the one that called the Function.

Exit Sub Immediately exits the Sub procedure in which it appears. Program execution proceeds with the statement following the one that has called the Sub.


Note: Exit Statements and End Statements are very different. An End statement defines the end of a structure; an Exit Statement executed to prematurely end a block of code.

See Also:

Do...Loop Statement
End Statement
Function Statement
If...Then...Else Statement
Select Case Statement
Sub Statement