Table Of Contents
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 subroutine 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.
How to Call Subroutine in Excel VBA?
Running the excel macro 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.
Code:
Sub Code_1() Range("A1").Value = "Hello" End Sub Sub Code_2() Range("A1").Interior.Color = rgbAquamarine End Sub
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”.
We will run the second code, “Code_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.
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.
Press the F8 key one more time. It will jump to the next line.
The yellow-colored line shows the highlighted code is about to execute if we press the F8 key again. Press the F8 key now.
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.
It has jumped to the mentioned sub procedure name. Press the F8 key once again.
Now, the actual task line highlights. To execute this, press the F8 key one more time.
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.