How to Call a Sub in VBA in Excel (4 Examples)

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

Call a Sub from Another Sub in VBA in Excel

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.

Sub with Arguments to Call a Sub from Another Sub in Excel

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)

VBA Code to Call a Sub from Another Sub in Excel

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.

Sub without Arguments to Call a Sub from Another Sub in Excel

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

VBA Code to Call a Sub from A Function in Excel

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.

VBA Code to Call a Sub from Another Sub in Excel

To call Sub1 from Function1, use the code:

 

Call Sub1(10)

VBA Code to Call a Sub from A Function in VBA in Excel

If we insert Function1 in any cell of the worksheet, it will show 10 in a Message Box.

Inserting Function to Call a Sub in VBA in Excel

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.

Output to Call a Sub from Another Sub in VBA in Excel

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

Get FREE Advanced Excel Exercises with Solutions!

Tags:

Rifat Hassan
Rifat Hassan

Rifat Hassan, BSc, Electrical and Electronic Engineering, Bangladesh University of Engineering and Technology, has worked with the ExcelDemy project for almost 2 years. Within these 2 years, he has written over 250 articles. He has also conducted a few Boot Camp sessions on effective coding, especially Visual Basic for Applications (VBA). Currently, he is working as a Software Developer to develop and deploy additional add-ins to enhance the customers with a more sophisticated experience with Microsoft Office Suits,... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo