EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home VBA VBA Resources VBA Coding VBA Exit Sub
 

VBA Exit Sub

Ashwani Jaiswal
Article byAshwani Jaiswal
Madhuri Thakur
Reviewed byMadhuri Thakur

VBA Exit Sub

Introduction to VBA Exit Sub

Exit Sub seems like ending the subcategory by its name. But in reality, Exit Sub is used to jump out of the sub procedure altogether without letting the complete code getting completed. When we apply Exit Sub in between any code then only code before Exit sub gets run completely in VBA Excel. The code after Exit sub will get skipped and it will jump the entire code after to that. Exit Sub usually works better with Do-While Loop.

 

 

How to Apply Exit Sub Using VBA?

Let’s see the examples of Exit Sub in Excel VBA.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

You can download this VBA Exit Sub Excel Template here – VBA Exit Sub Excel Template

Example #1 – VBA Exit Sub

Step 1: To apply Exit Sub we need a module. For that, go the VBA window. Click on Insert menu tab, we will get the list, from there select Module as shown below.

VBA Exit Module 1

Step 2: After that, a newly opened Module, write the subcategory in the name of VBA Exit Sub or in any other name as shown below.

Code:

Sub VBA_ExitSub1()

End Sub

VBA Exit Sub Example 1.1

Step 3: Define a variable Dim with any name, let’ say A. And give it Integer function to it as shown below.

Code:

Sub VBA_ExitSub1()

Dim A As Integer

End Sub

Excel VBA Exit Sub Example 1.2

Step 4: Now open a Do While Loop as shown below.

Code:

Sub VBA_ExitSub1()

Dim A As Integer
Do

While

End Sub

VBA Exit Sub Example 1.3

Step 5: In between Do While we write a simple code of comparison. Let’s DO addition of variable A with +1. Which means DO Addition for A with its own value +1. And run it till we get the +1 value for variable A.

Code:

Sub VBA_ExitSub1()

Dim A As Integer
Do
A = A + 1

While

End Sub

VBA Sub Example 1.4

Step 6: Now how much we want to add +1 to A will be defined in Loop While condition. Which says DO Loop will run following the condition of A + 1 while A is less than 100.

Code:

Sub VBA_ExitSub1()

Dim A As Integer
Do
A = A + 1

Loop While A < 100

End Sub

Sub Example 1.6

Step 7: Now we will need a message box (Which is optional) to see the output of Do While loop. For that insert MsgBox and give it to the variable A.

Code:

Sub VBA_ExitSub1()

Dim A As Integer
Do
A = A + 1

Loop While A < 100
MsgBox A

End Sub

 Example 1.6

Step 8: Now compile the code by pressing the F8 key to see if there is any bug in the code or not. Later run the entire code by pressing the Play button located below the menu bar as shown below. We will see the message box has given us the output as 100. Which means a value of A is less than 100 and formula which we defined in DO Loop says is the value of A is A + 1. So the answer is coming as 100.

VBA Exit Sub 1

Step 9: Now to apply the Exit Sub in performed Do While loop we need to add Exit Sub statement before Do Loop start or after we defined variable A as shown below.

Code:

Sub VBA_ExitSub1()

Dim A As Integer
Exit Sub
Do
 A = A + 1

Loop While A < 100
MsgBox A

End Sub

VBA 1.7

Now again compile the code and run, if no error found.

We will observe that we have not found any output. Which is because the code run is completed but it has skipped off to Do While loop completely giving no output in a message box.

Example #2 – VBA Exit Sub

Let’s consider another example. In this example, we will see how Exit Sub works on skipping the font color change. For we have some text at cell C3 as shown below. As we can see the text is in default blank color.

 VBA Exit Sub Example 2

Step 1: For this open a new module and give it Sub-Category in the name of VBA Exit sub or in any other suitable name as per your need.

Code:

Sub VBA_ExitSub2()

End Sub

Exit Example 2.1

Step 2: Select the range cell which has the text. Here our range cell will be cell C3.

Code:

Sub VBA_ExitSub2()

Range("C3").Select

End Sub

VBA Example sub 2.2

Step 3: As we need to change the font color, so in the next line of code select the Font and Color function simultaneously as shown below.

Code:

Sub VBA_ExitSub2()

Range("C3").Select
Selection.Font.Color =

End Sub

Example 2.3

Step 4: Now choose the color of font which we want to change. And the selection of color will be starting with vb as per VBA setting. Here, we are selecting Red as shown below.

Code:

Sub VBA_ExitSub2()

Range("C3").Select
Selection.Font.Color = vbRed

End Sub

Sub Example 2.5

Step 5: Now compile the entire the code in one go as the code is quite small and run it. We will see the color of the font at cell C3 is now changed to Red color from default Black.

VBA sub Exit 2

Step 6: Now to apply the Exit Sub here in font color change code, put the statement of Exit Sub before the Range cells selection line of code or between range cell selection (1st) and Font color (2nd) as shown below.

Code:

Sub VBA_ExitSub2()

Range("C3").Select

Exit Sub

Selection.Font.Color = vbRed

End Sub

 Sub Exit Example 2.3

Step 7: Now again run the code. This time we will notice that the font color of text at cell C3 doesn’t get changed to Red color as it changed earlier before placing Exit Sub statement.

VBA excel output

Step 8: We can convert the added Exit Sub statement into text by inserting a single inverted quote as shown below. By doing this, it will get converted into text. After that, the color of that line will be get changed to Green color.

Code:

Sub VBA_ExitSub2()

Range("C3").Select

'Exit Sub

Selection.Font.Color = vbRed

End Sub

VBA Green Sub

Pros of VBA Exit Sub

  • It saves time in re-writing a code again and again.
  • We can skip the portion of code which we don’t want to run.
  • It is applicable in all types of functions.
  • This is quite useful when working on big lines of code where we need to skip or jump some certain line of code.

Things to Remember

  • VBA Exit Sub works with all kind of codes and function but it works better with all kind of Loop, especially DO-WHILE Loop.
  • It jumps out from the portion of code and running only that much code which is before Exit Sub.
  • Exit and End Sub, both are different.
  • It skips and jumps out of the code without running it completely. And half run code doesn’t give any proper output.

Recommended Articles

This is a guide to VBA Exit Sub. Here we discuss how to use Excel VBA Exit Sub along with few practical examples and downloadable excel template. You can also go through our other suggested articles –

  1. VBA While Loop
  2. VBA Remove Duplicates
  3. VBA Data Types
  4. VBA Sleep

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Web Development & many more.

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

Download VBA Exit Sub Excel Template

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW