Example 1 – Call a Sub without Arguments from Another Sub in VBA in Excel
We will call a Sub without any argument from another Sub in VBA.
Sub1 is the Sub without arguments.
We’ll call the Sub1 from another Sub called Sub2.
To call Sub1 from Sub2, the code is:
Sub1
Or
Call Sub1
If you run Sub2, Sub1 will be called and the message “Sub1 is Run.” will be displayed.
Read More: Excel VBA Call Sub from Another Module
Example 2 – Call a Sub with Arguments from Another Sub in VBA in Excel
We’ll call a Sub with arguments from another Sub in VBA.
We’ve modified Sub1 to include an argument named Input_Value. When you run the code, it will display the argument.
To call Sub1 from another Sub (Sub2), use the following code:
Sub1(Input_Value)
Or
Call Sub1(Input_Value)
We’ve used:
Call Sub1(10)
When you run Sub2, Sub1 will be called with the input 10, and 10 will be displayed in a Message Box.
Read More: VBA to Call Sub From Another Workbook in Excel
Example 3 – Call a Sub with/without Arguments from A User-Defined Function in VBA in Excel
You can also call a Sub from a User-Defined Function in VBA.
⧭ Sub without Arguments
We’ve modified Sub1 to one without arguments.
We’ll create a Function called Function1 and call Sub1 from that function.
To call Sub1 from a function, the line of code is:
Sub1
Or
Call Sub1
If you insert Function1 in any cell of your worksheet, Sub1 will be called and a Message Box will display “Sub1 is Run.”.
⧭ Sub with Arguments
We’ve modified Sub1 to one with arguments.
To call Sub1 from Function1, use the code:
Call Sub1(10)
If we insert Function1 in any cell of the worksheet, it will show 10 in a Message Box.
Read More: Excel VBA Call Sub from Another Sheet
Example 4 – Call a Private Sub from Another Sub or Function in VBA in Excel
⧭ Calling from a Sub:
You can only call a Private Sub from another Sub if the two are in the same module of the VBA window.
We’ve changed Sub1 to a Private Sub by adding the term Private in the first line and we will call it from Sub2 which is in the same module.
If you run Sub2, you will get a Message Box displaying 10.
⧭ Calling from a Function:
To call a Private Sub from a Function in VBA, the Sub and the Function must be in the same module.
We’ve inserted both the Private Sub (i.e. Sub1) and the Function (i.e. Function1) in the same module.
If we insert Function1 in any cell of the worksheet, a Message Box will display 10.
Download Practice Workbook
Further Readings
- How to Call Private Sub in Excel VBA
- How to Use Excel VBA to Call Private Sub from Userform
- Excel VBA to Call Sub with Parameters
- How to Run a Private Sub in VBA