The Exit Statement | VBA Jump Statements

Here, you'll explore the Exit statement which instantly leaves a block. It is one of several VBA jump statements that enables non-sequential code execution.

In this article:

5 minutes read

VBA programs execute code-lines in a sequential manner. However, situations often arise where you need to skip ahead or circle back to a code-line. That is where jump statements like the Exit statement come into play. They are of immense importance as they allow the non-sequential transfer of control (i.e., program execution).

Introducing the Exit Statement

The Exit statement instantly leaves a code-block. Also, it transfers control to the code-line right after the block’s closing statement.

However, it only works for specific blocks. These include Do and For loops, and Function, Subroutine (i.e., Sub), and Property procedures. The table below shows its syntax for each of these blocks.

SyntaxImmediately Exits FromTransfers Control To
Exit DoDo WhileLoop statement;
Do UntilLoop statement;
DoLoop While statement;
DoLoop Until statement.
Code-line directly following the block’s closing statement, i.e. Loop, Loop While, or Loop Until.
Exit ForFor Next statement;
For EachNext statement.
Code-line directly following the block’s closing statement, i.e. Next.
Exit FunctionFunction statementCode-line directly following the statement that invoked (i.e., called) the Function.
Exit SubSub statementCode-line directly following the statement that invoked (i.e., called) the Subroutine.
Exit PropertyProperty statementCode-line directly following the statement that invoked (i.e., called) the Property.

Use in other Jump Statements

By themselves, Exit statements rarely prove useful. They are typically encased in Selection statements that set conditions for their execution. It is common to find them in other types of jump statements.

Exit statement within the GoSub Statement

We have detailed the GoSub statement in a separate article. The image below (taken from that article) shows its logic flowchart. The Exit statement’s utility as part of the GoSub statement is clear.

Flowchart showing the Exit statement's utility as part of the GoSub – Return statement’s logic flow.
Flowchart showing the Exit statement’s utility as part of the GoSub – Return statement’s logic flow

Exit statement within the On – GoSub Statement

We have explored the On GoSub statement in another article. The image below (taken from that article) shows its logic flowchart. The Exit statement’s usefulness as part of the On GoSub statement stands out.

Flowchart showing the Exit statement's utility as part of the On – GoSub statement’s logic flow.
Flowchart showing the Exit statement’s utility as part of the On – GoSub statement’s logic flow

Exit statement within the On Error GoTo Statements

The On Error GoTo Line/Label statements are explored in a standalone article. The images below (taken from that article) show their logic flowcharts. You can see the importance of the Exit statement as part of the On Error GoTo Line/Label statements.

Flowchart showing the Exit statement's utility as part of the On Error GoTo line – Resume Next statements’ logic.
Flowchart showing the Exit statement’s utility as part of the On Error GoTo line – Resume Next statements’ logic
Flowchart showing the Exit statement's utility as part of the On Error GoTo line – Resume line statements’ logic.
Flowchart showing the Exit statement’s utility as part of the On Error GoTo line – Resume line statements’ logic
Flowchart showing the Exit statement's utility as part of the On Error GoTo line – Resume 0 statements’ logic.
Flowchart showing the Exit statement’s utility as part of the On Error GoTo line – Resume 0 statements’ logic

Use in Iteration Statements

The Exit statement also finds common use as part of VBA iteration statements. There are two types of VBA iteration statements, For and Do loops.

Exit statement within For Loops

The For – Next statement is detailed in a separate article. The image below (taken from that article) shows its logic flowchart. The Exit statement’s utility as part of the For – Next statement is clear.

Flowchart showing the Exit statement's utility as part of the For – Next statement’s logic flow.
Flowchart showing the Exit statement’s utility as part of the For – Next statement’s logic flow

The For – Each statement is discussed in-depth in a standalone article. The image below (taken from that article) shows its logic flowchart. The Exit statement’s usefulness as part of the For – Each statement stands out.

Flowchart showing the Exit statement's utility as part of the For Each – Next statement’s logic flow.
Flowchart showing the Exit statement’s utility as part of the For Each – Next statement’s logic flow

Exit statement within the Do Loops

We have explored the Do While and Do Until statements in another article. The images below (taken from that article) show their logic flowcharts. You can see the importance of the Exit statement as part of these iteration statements.

Flowchart showing the Do While – Loop statement’s logic flow.
Flowchart showing the Exit statement’s utility as part of the Do While – Loop statement’s logic flow
Flowchart showing the Do Until – Loop statement’s logic flow.
Flowchart showing the Exit statement’s utility as part of the Do Until – Loop statement’s logic flow

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Keep Learning

Related Articles

Keep Learning

Related Articles