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.
You are free to use this image on you website, templates, etc., Please provide us with an attribution linkHow to Provide Attribution?Article Link to be HyperlinkedFor eg:Source: VBA Call Sub (wallstreetmojo.com)
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.
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.
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.
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: –
- Excel VBA Tutorial for BeginnersExcel VBA Tutorial For BeginnersVisual Basic for Applications (VBA) tutorial provides extensive learning of this Microsoft programming language for its application on Microsoft products like Excel, Word, and PowerPoint. It facilitates the user to do the programming in the Visual Basic Editor (VBE), a platform to write codes for executing various tasks in excel.read moreVBA EndVBA EndEnd is a VBA statement that can be used in a variety of ways in VBA applications. Anywhere in the code, a simple End statement can be used to instantly end the execution of the code. In procedures, the end statement is used to end a subprocedure or any loop function, such as ‘End if’.read moreToday in VBAToday In VBAVBA Today refers to the current date. In the worksheet, the function does the same thing and provides the current date and time. The method to get the current date is by using the date function, and unlike the now function, the date function only gives us the current date.read moreSubscript Out of Range in VBASubscript Out Of Range In VBASubscript out of range is an error in VBA that occurs when we attempt to reference something or a variable that does not exist in the code. For example, if we do not have a variable named x but use the msgbox function on x, we will receive a subscript out of range error.read more