WallStreetMojo

WallStreetMojo

WallStreetMojo

MENUMENU
  • Free Tutorials
  • Certification Courses
  • Excel VBA All in One Bundle
  • Login
Home » Excel, VBA & Power BI » Learn VBA » VBA Return

VBA Return

Excel VBA Return Statement

VBA is wonderful and a lifesaver for those who know the in and out of VBA coding. It can save tons of time at our workplace. In our earlier articles, we have discussed many things and concepts of VBA return. One such concept is the GoSub Return statement. In this article, we will introduce you to these concepts.

VBA Return

What is GoSub Return Statement?

The statement “Go Sub” means it will go to the line of code indicated by label through name and performs a specific set of the task until it finds the statement “Return.”

This is similar to the error handler statement “GoTo Label,” but what “GoSub Return” does is it will return back to the next line of code rather than continuing from the label name.

Below is the syntax of the VBA GoSub Return statement.

GoSub [Label Name]
….. Line of Code
[Label Name]:
….. Line of Code to perform the task

I know it is difficult to understand from reading the syntax but using the example, you may be able to understand the concept.

Before I go-to examples, let me tell a few things about this statement.

  • GoSub statement calls subroutine in VBA, which is named by a label within the same routine or Function.
  • Both GoSub and Return should be within the same procedure. We cannot call another subroutine macro here.
  • You can introduce any number of GoSub Return statements.
  • A return statement will continue the execution of the code from where it left off before it jumped to the subprocedure indicated by a label name.

How to use GoSub Return Statement in VBA?

You can download this VBA Return Excel Template here – VBA Return Excel Template

Example #1

To understand the usage of this statement, look at the below code first. Later I will explain to you the code line by line.

Code:

Sub Go_Sub_Return()

  GoSub Macro1 'Label Name1
  GoSub Macro2 'Label Name2
  GoSub Macro3 'Label Name3

  Exit Sub

Macro1:
  MsgBox "Now running Macro1"
Return

Macro2:
  MsgBox "Now running Macro2"
Return

Macro3:
  MsgBox "Now running Macro3"
Return

End Sub

To understand this code, let’s run the code line by line by pressing the F8 key. Upon pressing the F8 key first time, it will initiate the macro running.

VBA Return Example 1

Now press the F8 key one more time to go to the next line.

Popular Course in this category
Sale
All in One Excel VBA Bundle (35 Courses with Projects)
4.9 (1,353 ratings)
35+ Courses | 120+ Hours | Full Lifetime Access | Certificate of Completion
View Course

VBA Return Example 1-1

We all know macro will run the code line by line, but if you press the F8 key, it will not go to the next line; rather, it works differently.

VBA Return Example 1-2

It has jumped to the label named as “Macro1” this is because in the previous statement “GoSub,” we have instructed the macro to go to the subprocedure label name “Macro1”, so accordingly, it has jumped to the respective label name.

Now by pressing the F8 key, excel macro will execute the label “Macro1” task of showing the result in the message box.

VBA Return Example 1-3

Click on the Ok of the message box to return to the coding window.

VBA Return Example 1-4

Now it had highlighted the “Return” statement. If you press the F8 key one more time, what it will do is it will “Return Back” to the previous line of code before it jumped to the label name.

VBA Return Example 1-5

Last time it executed the code “GoSub Macro1,” and it performed the label name “Macro1” task. Since we have mentioned the “Return” statement, it is back to the next line of code i.e.

“GoSub Macro2”

What is statement says is, “go-to label named Macro2”. In the below for label “Macro2”, I have mentioned a specific set of tasks.

VBA Return Example 1-6

Now we will get the second subprocedure task of showing value in the message box.

VBA Return Example 1-7

Now click on, Ok. It will highlight the second “Return” statement.

VBA Return Example 1-8

Now upon clicking the F8 key, it will return to the next line of code before it returns to the label “Macro2”. Now it will highlight “GoSub Macro3”.

VBA Return Example 1-9

Now it will go to the label “Macro3”, press the F8 key.

Example 1-10

Now third label task will be executed.

Example 1-11

Now click on, OK. It will highlight the “Return” statement.

Example 1-12

Now press the F8 key one more time; it will jump back to the line of code “Exit Sub.”

Example 1-13

No, it will exit the subroutine. It is necessary to add the word “Exit Sub” otherwise, and it will encounter an error.

Example #2

Now take a look at one more example of using the GoSub Return statement.

Code:

Sub Go_Sub_Return1()

  Dim Num As Long

  Num = Application.InputBox
  (Prompt:="Please enter the number here", Title:="Divsion Number")

  If Num > 10 Then
  GoSub Division
  Else
    MsgBox "Number is less than 10"
    Exit Sub
  End If
  Exit Sub

Division:
  MsgBox Num / 5
Return

End Sub

This code will ask you to enter the number which is >10. If the number is >10, then it will perform the task of dividing it by 5 (Label Name is “Division”), or else it will show the message as “Number is less than 10”.

VBA Return Example 2

Things to Remember

  • The label name should be the same in GoSub and Return statement, and in the return statement label name should be followed by a colon (:).
  • Always use Exit Sub in VBA after all the return statements are made to avoid the error message.

Recommended Articles

This has been a guide to VBA Return. Here we learn how to use GoSub Return Statement in VBA along with examples and a downloadable excel template. Below are some useful excel articles related to VBA –

  • VBA IsDate
  • VBA VARTYPE Examples
  • Switch in VBA
  • Filter in VBA
0 Shares
Share
Tweet
Share
All in One Excel VBA Bundle (35 Courses with Projects)
  • 35+ Courses
  • 120+ Hours
  • Full Lifetime Access
  • Certificate of Completion
LEARN MORE >>
Primary Sidebar
Footer
COMPANY
About
Reviews
Contact
Privacy
Terms of Service
RESOURCES
Blog
Free Courses
Free Tutorials
Investment Banking Tutorials
Financial Modeling Tutorials
Excel Tutorials
Accounting Tutorials
Financial Statement Analysis
COURSES
All Courses
Financial Analyst All in One Course
Investment Banking Course
Financial Modeling Course
Private Equity Course
Venture Capital Course
Excel All in One Course

Copyright © 2021. CFA Institute Does Not Endorse, Promote, Or Warrant The Accuracy Or Quality Of WallStreetMojo. CFA® And Chartered Financial Analyst® Are Registered Trademarks Owned By CFA Institute.
Return to top

WallStreetMojo

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

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

Book Your One Instructor : One Learner Free Class
Let’s Get Started
Please select the batch
Saturday - Sunday 9 am IST to 5 pm IST
Saturday - Sunday 9 am IST to 5 pm IST

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

WallStreetMojo

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

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

Login

Forgot Password?

WallStreetMojo

Download VBA Return Excel Template

New Year Offer - All in One Financial Analyst Bundle (250+ Courses, 40+ Projects) View More