VBA Call Sub

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

What is Call Sub in VBA?

We can execute all the sub-procedures of the same module in a single subroutine, and the process of executing them in a single VBA subroutineVBA SubroutineSUB in VBA is a procedure which contains all the code which automatically gives the statement of end sub and the middle portion is used for coding. Sub statement can be both public and private and the name of the subprocedure is mandatory in VBA.read more is called “Call Sub.”

Sometimes, we may need to write a huge amount of code, and writing them in a single macro creates many problems while debugging the code. Unfortunately, at the start, everybody tends to do this purely because of the lack of knowledge of the “Call Sub” method.

Keeping all the codes in a single sub procedure is not a good practice. Instead, we need to break them into multiple sub procedures to simplify the code.

VBA Call Sub

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 Call Sub (wallstreetmojo.com)

–>> 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 Call Subroutine in Excel VBA?

Running the 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 from one procedure to another makes life easier purely based on saving a lot of time while running and debugging the code in case of any error.

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

Code:

Sub Code_1()

  Range("A1").Value = "Hello"

End Sub

Sub Code_2()

  Range("A1").Interior.Color = rgbAquamarine

End Sub
VBA Call Sub Example 1

In the above image, we have two subprocedures. The first one is “Code_1,” and the second one is “Code_2”.

In the first VBA call subcode, we just wrote a code to insert a value to cell A1 as “Hello.” In the second sub procedure, we have written the code to change the interior color of cell A1 to “rgbAquamarine.”

We will run the first code, “Code_1”.

VBA Call Sub Example 1-1

We will run the second code, “Code_2”.

VBA Call Sub Example 1-2

Here, we have executed the code times.

By using the VBA “call Sub,” we can execute both the sub procedures in a single macro. First, we need to add the word “Call,” followed by a macro name.

Look at the below graphic picture.

VBA Call Sub Example 1-3

We have only mentioned the code as “Call Code_2” in the first subprocedure. Now to understand, let us run the code line by line. Press the F8 key. It will highlight the macro name.

VBA Call Sub Example 1-4

Press the F8 key one more time. It will jump to the next line.

VBA Call Sub Example 1-5

The yellow-colored line shows the highlighted code is about to execute if we press the F8 key again. Press the F8 key now.

Example 1-6

As we can see, it has inserted the word “Hello” into cell A1. So now, the “Call Code_2” line highlights.

“Call Code_2” has the task of changing the interior color of cell A1 and the word “Call Code_2” will execute this code from the actual sub procedure only.

But press the F8 key to see the magic.

Example 1-7

It has jumped to the mentioned sub procedure name. Press the F8 key once again.

Example 1-8

Now, the actual task line highlights. To execute this, press the F8 key one more time.

Example 1-9

Like this, we can execute many sub-procedures from one sub procedure by calling the sub procedure by its name “Call.”

Note:

  • We can execute the macro of another sub procedure without using the word “Call” but just by mentioning the macro name itself.
  • It is not the best practice because if the macro sub procedure contains parenthesis that you want to execute, then the “Call” word is mandatory.
  • We always think of using the word “Call” because it is just a four-letter word, allowing others to understand the code correctly.

Recommended Articles

This article has been a guide to Excel VBA Call Sub. Here, we learn how to call a subroutine in Excel VBA, practical examples, and a downloadable Excel template. In addition, below are some useful Excel articles related to VBA: –