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. 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.
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.
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 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”.
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 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 one more time. Press the 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 subprocedure name. Press the 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 subprocedure by calling the subprocedure 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.