Excel VBA Call Sub (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 subroutine called “Call Sub” in Excel VBA.
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 the good practice of keeping all the codes in a single sub procedure, we need to break them into multiple sub procedures to simplify the code.
How to Call Subroutine in Excel VBA?
Running the 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.
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, I have just written a code to insert a value to the cell A1 as “Hello”. In the second subprocedure, 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”.
Now I will run the second code i.e. “Code_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.
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.
Press 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 one more time. Press F8 key now.
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.
It has jumped to the mentioned sub procedure name. Press F8 key once again.
Now the actual task line highlighted, to execute this press F8 key one more time.
Like this, we can execute many sub-procedures from one sub procedure by calling the sub procedure by their name with the word “Call”.
- 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.
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 –
- VBA RegEx Pattern
- Change VBA Font Color | Using RGB Function
- How to use the VBA WorkBook Object?
- VBA Split String
- Excel VBA Tutorial for Beginners
- End Function in VBA
- Today in VBA
- Clear Contents in VBA
- SubString in VBA
- Subscript Out of Range in VBA
- 35+ Courses
- 120+ Hours of Videos
- Full Lifetime Access
- Certificate of Completion
- Basic Excel Training
- Advanced Excel Training
- Basic & Advanced VBA Course
- Excel Dashboard Course
- Data Analysis in Excel
- Create VBA Applications