VBA Call Sub

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 called “Call Sub.”

In some of the cases, we may need to write a huge amount of code to and writing them in a single macro creates lots of problems while debugging the code. At the start, everybody tends to do this purely because of the lack of knowledge on the “Call Sub” method.

It is not a good practice of keeping all the codes in a single subprocedure. 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)

How to Call Subroutine in Excel VBA?

Running the excel macro from one procedure to another make life easier for purely on the basis of saving a lot of time while running as well a while 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, I have just written a code to insert a value to the cell A1 as “Hello.” In the second sub procedure, I have written the code to change the interior color of the cell A1 to “rgbAquamarine.”

Now I will run the first code, i.e., “Code_1”.

VBA Call Sub Example 1-1

Now I will run the second code, i.e., “Code_2”.

VBA Call Sub Example 1-2

Here I have executed the code times.

By using the VBA “call Sub,” we can execute both the subprocedure in a single macro only. We just need to add the word “Call,” followed by a macro name.

Look at the below graphic picture.

VBA Call Sub Example 1-3

I have mentioned the code as “Call Code_2” in the first subprocedure only. Now to understand, let’s 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 one more time. Press the F8 key now.

Example 1-6

As we can see, it has inserted the word “Hello” to the cell A1. Now the “Call Code_2” line has been highlighted.

“Call Code_2” has the task of changing the interior color of the 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 subprocedure name. Press the F8 key once again.

Example 1-8

Now the actual task line highlighted, to execute this press F8 key one more time.

Example 1-9

Like this, we can execute many sub-procedures from one subprocedure by calling the subprocedure by their name with the word “Call.”

Note:

  • We can execute the macro of another sub procedure without using the word “Call” but just by mentioning the macro name itself.
  • This is not the best practice because if the macro sub procedure contains parenthesis that you want to execute, then the “Call” word is mandatory.
  • In my personal opinion, always use the word “Call” because it is just a 4 letter word which allows others to understand the code correctly.

Recommended Articles

This has been a guide to Excel VBA Call Sub. Here we learn how to Call Subroutine in excel VBA along with practical examples and a downloadable excel template. Below are some useful excel articles related to VBA.

  • 3 Courses
  • 12 Hands-on Projects
  • 43+ Hours
  • Full Lifetime Access
  • Certificate of Completion
LEARN MORE >>