VBA Return

Updated on January 2, 2024
Article byJeevan A Y
Edited byAshish Kumar Srivastav
Reviewed byDheeraj Vaidya, CFA, FRM

Excel VBA Return Statement

VBA is wonderful and a lifesaver for those who know the in and out of VBA codingVBA CodingVBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task.read more. It can save tons of time at our workplace. In our earlier articles, we 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

You are free to use this image on your website, templates, etc, Please provide us with an attribution linkHow to Provide Attribution?Article Link to be Hyperlinked
For eg:
Source: VBA Return (wallstreetmojo.com)

What is a GoSub Return Statement?

The statement “GoSub” 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.”

It is similar to the error handler statement “GoTo Label,” but what “GoSub Return” does is return 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

We know it is not easy to understand from reading the syntax but using the example, you may be able to understand the concept.

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

–>> If you want to learn Excel VBA professionally, then our VBA Basic Course (16+ hours) is the perfect solution. In our Basic Excel VBA course you learn the skill of automating tasks using Variables, Data Types, Ranges, and Cells in VBA. Master Control Structures, including Conditional Statements and Loops, and discover techniques for manipulating data through sorting, filtering, and formatting. By the end of the course, you will be able to apply your acquired skills to real projects, culminating in an Excel VBA project which will solidify your ability to create efficient, automated solutions.

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, we 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

Let us run the code by pressing the F8 key to understanding this code. After pressing the F8 key first, it will initiate the macro running.

VBA Return Example 1

Now, press the F8 key once more to go to the next line.

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 “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.

By pressing the F8 key, excel macroExcel MacroA macro in excel is a series of instructions in the form of code that helps automate manual tasks, thereby saving time. Excel executes those instructions in a step-by-step manner on the given data. For example, it can be used to automate repetitive tasks such as summation, cell formatting, information copying, etc. thereby rapidly replacing repetitious operations with a few clicks. read more will execute the label “Macro1” task of showing the result in the message box.

VBA Return Example 1-3

Click on the “OK” message to return to the coding window.

VBA Return Example 1-4

Now, it has highlighted the “Return” statement. So, 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 jumps to the label name.

VBA Return Example 1-5

The last time it executed the code “GoSub Macro1,” 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 the statement says is, “go-to label named Macro2”. In the below label “Macro2”, we 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

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

VBA Return Example 1-9

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

Example 1-10

Now, it will execute the third label task.

Example 1-11

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

Example 1-12

Now, press the F8 key again. It will jump back to the line of code “Exit Sub.”

Example 1-13

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

Example #2

Now, 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 the GoSub and Return statement, and in the return statement label name should be followed by a colon (:).
  • We must always use Exit Sub in VBA after making all the return statements to avoid the error message.

Recommended Articles

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