How to Call Sub in VBA
- Understanding Sub Procedures in VBA
- Calling a Sub Directly
- Calling a Sub with Arguments
- Calling a Sub from an Event
- Conclusion
- FAQ

When diving into the world of Visual Basic for Applications (VBA), understanding how to call a Sub procedure is essential for any developer. Whether you’re automating tasks in Excel, Access, or other Office applications, mastering this skill can significantly enhance your productivity.
In this article, we will demonstrate how to call a Sub in VBA effectively, providing clear examples and detailed explanations to ensure you grasp the concept fully. By the end, you’ll be equipped with the knowledge to implement your own procedures seamlessly. So, let’s get started!
Understanding Sub Procedures in VBA
Sub procedures, or simply “Subs,” are blocks of code designed to perform specific tasks in VBA. They can be called from other Sub procedures, functions, or even triggered by events, such as clicking a button. The syntax for declaring a Sub is straightforward: you use the Sub
keyword followed by the name of the procedure and parentheses.
Here’s a simple example of a Sub procedure:
Sub HelloWorld()
MsgBox "Hello, World!"
End Sub
In this example, the HelloWorld
Sub displays a message box with the text “Hello, World!” When you want to execute this Sub, you simply call it by its name:
Sub CallHelloWorld()
HelloWorld
End Sub
This code snippet demonstrates how to call the HelloWorld
Sub from another Sub named CallHelloWorld
. When CallHelloWorld
runs, it triggers HelloWorld
, resulting in the message box appearing. This basic understanding sets the foundation for more complex procedures and interactions.
Calling a Sub Directly
One of the simplest ways to call a Sub is to do it directly within another Sub. This method is particularly useful for executing a series of tasks sequentially.
Here’s an example of how to call a Sub directly:
Sub MainProcedure()
Call FirstTask
Call SecondTask
End Sub
Sub FirstTask()
MsgBox "This is the first task."
End Sub
Sub SecondTask()
MsgBox "This is the second task."
End Sub
In this code, the MainProcedure
Sub calls two other Subs, FirstTask
and SecondTask
, in sequence. The Call
keyword is optional; you can simply write the Sub name if you prefer.
When you run MainProcedure
, it will first show a message box for the first task, and after you close that message box, it will show the second task’s message box.
Output:
This is the first task.
Output:
This is the second task.
This approach is efficient for organizing code and ensuring that tasks execute in the desired order. It also improves readability, making it easier for you and others to understand what the code is doing.
Calling a Sub with Arguments
Sometimes, you may need to pass information to a Sub to customize its behavior. In such cases, you can define parameters in the Sub’s declaration and pass arguments when calling it.
Here’s an example:
Sub GreetUser(name As String)
MsgBox "Hello, " & name & "!"
End Sub
Sub CallGreetUser()
Call GreetUser("Alice")
End Sub
In this example, the GreetUser
Sub takes one argument, name
. When you call GreetUser
from CallGreetUser
, you provide the name “Alice” as an argument. The result is a personalized greeting message.
Output:
Hello, Alice!
By using arguments, you can create more dynamic and reusable Subs. This flexibility allows you to adapt your procedures to various situations, making your code more versatile and efficient.
Calling a Sub from an Event
In VBA, you can also call Subs in response to events, such as clicking a button on a form or worksheet. This interaction can greatly enhance the user experience by allowing for immediate feedback and functionality.
Here’s how to set it up:
- Create a button on your Excel worksheet.
- Assign the following Sub to the button click event:
Sub ButtonClick()
Call ShowMessage
End Sub
Sub ShowMessage()
MsgBox "Button clicked!"
End Sub
In this scenario, when the user clicks the button, the ButtonClick
Sub is triggered, which in turn calls the ShowMessage
Sub. This results in a message box appearing with the text “Button clicked!”
Output:
Button clicked!
This method of calling a Sub enhances interactivity in your applications. By associating Subs with events, you can create a responsive environment that reacts to user actions, making your applications more engaging.
Conclusion
Calling a Sub in VBA is a fundamental skill that can streamline your coding process and make your applications more functional. Whether you are calling a Sub directly, passing arguments, or tying your Subs to events, mastering these techniques will significantly improve your VBA programming. As you continue to explore and experiment with VBA, remember that practice is key. The more you work with Subs, the more comfortable you’ll become in leveraging their power to automate and enhance your workflows.
FAQ
-
What is a Sub in VBA?
A Sub, or Subroutine, is a block of code in VBA that performs a specific task and can be called from other parts of your code. -
How do I call a Sub in VBA?
You can call a Sub by simply using its name followed by parentheses. Optionally, you can use theCall
keyword before the Sub name. -
Can I pass arguments to a Sub in VBA?
Yes, you can define parameters in the Sub declaration and pass arguments when calling the Sub to customize its behavior. -
What is the difference between a Sub and a Function in VBA?
A Sub performs actions and does not return a value, while a Function performs actions and returns a value. -
Can I call a Sub from an event in VBA?
Yes, you can call a Sub in response to events, such as button clicks, to create interactive applications.