To call a sub in VBA, you simply type its name followed by its arguments (if any) on a new line, or use the Call keyword followed by the sub name and its arguments in parentheses. For example, MySub or Call MySub(1, "text"). This article explains the syntax, when to use Call, how to pass arguments, and common pitfalls.
What Is a Sub in VBA?
A Sub (short for Subroutine) is a procedure that performs a specific task but does not return a value. Unlike a Function, which returns a result that you can assign to a variable, a Sub is called for its side effects—like modifying a cell, showing a message, or running a calculation that stores results elsewhere. Subs are the workhorses of VBA macros, often triggered by buttons, events, or other procedures.
Basic Syntax for Calling a Sub
There are two primary ways to call a Sub in VBA:
- Direct call (no parentheses): Type the sub name followed by a space and any arguments separated by commas. For example:
MySub 1, "Hello". This works only when the call is a standalone statement, not part of an expression. - Using the
Callkeyword: TypeCallfollowed by the sub name and arguments enclosed in parentheses. For example:Call MySub(1, "Hello"). TheCallkeyword is optional in modern VBA but is still used for clarity or when you need parentheses for readability.
Here is a complete example:
Sub Main()
' Direct call
Greet "Alice"
' Using Call keyword
Call Greet("Bob")
End Sub
Sub Greet(name As String)
MsgBox "Hello, " & name
End Sub
Both calls display a message box. Note that when using the direct call, you do not use parentheses around the arguments; if you do, VBA treats it as a function call and may cause a syntax error unless you use Call.
Calling a Sub with Arguments
Arguments can be passed by reference (the default) or by value. By default, VBA passes arguments by reference, meaning the called sub can modify the original variable. To pass by value, use the ByVal keyword in the sub’s parameter list. When calling, you can also force passing by value by enclosing the argument in extra parentheses, but this is rarely needed.
Example with multiple arguments:
Sub CalculateSum(a As Integer, b As Integer)
MsgBox a + b
End Sub
Sub Test()
CalculateSum 5, 10 ' Direct call
Call CalculateSum(5, 10) ' Call keyword
End Sub
Both produce 15. If you have optional parameters, you can omit them or use named arguments like SubName arg1:=value.
Calling a Sub from Another Module or Workbook
By default, Subs are Public, meaning they can be called from anywhere in the same project. To call a Sub from another module, you simply use its name if it is unique. If there are naming conflicts, you can qualify it with the module name, like Module1.MySub. To call a Sub from another workbook, you must reference that workbook’s VBA project (via Tools > References) and then use the full path, e.g., Workbooks("Other.xlsm").VBAProject.Module1.MySub—but this is rare and often requires enabling access to the VBA object model.
Common Errors and How to Avoid Them
Here are the most frequent mistakes when calling Subs:
- Using parentheses without
Call:MySub(1)is interpreted as passing the value 1 by value in some contexts, but if the sub expects an argument, this can cause a compile error. Always useCall MySub(1)orMySub 1. - Mismatched argument types: Passing a string when a number is expected causes a type mismatch. Ensure the data types align.
- Calling a Sub from a function context: You cannot use a Sub call in an expression like
x = MySubbecause Subs do not return values. Use a Function instead. - Forgetting to qualify when ambiguous: If two modules have a Sub with the same name, VBA may raise an “Ambiguous name” error. Use the module name prefix.
Using Call vs. Not Using It
The Call keyword is optional and does not change functionality. However, it forces you to use parentheses, which can make code more readable when arguments are long expressions. Some programmers prefer to omit Call for a cleaner look. There is no performance difference. The only time Call is required is when you want to call a Sub whose name is the same as a keyword (e.g., Call Date would be ambiguous), but that is extremely rare.
Calling a Sub from an Event or Button
You can assign a Sub to a button’s Click event by selecting the button in Design Mode and writing code in its Click event, or by using Application.Run for dynamic calls. For example, to call a Sub from a CommandButton named cmdRun, you would write in the button’s click event:
Private Sub cmdRun_Click()
MySub
End Sub
Alternatively, you can use Application.Run "MySub" to call a Sub by name as a string, which is useful when the sub name is stored in a variable.
Frequently Asked Questions
Can I call a Sub from a Function? Yes, you can call a Sub from within a Function. The Sub runs, and the Function continues. However, the Sub cannot return a value to the Function.
What is the difference between a Sub and a Function? A Sub performs actions and does not return a value; a Function returns a value and can be used in expressions. Use a Sub when you don’t need a result, and a Function when you do.
How do I call a Sub from another workbook? You need to set a reference to that workbook’s VBA project (Tools > References, then check the workbook) and then use the full module path. This is advanced and requires trust settings.
Why do I get a compile error when I use parentheses? If you write MySub(1) without Call, VBA interprets it as passing the value 1 by value, which is fine, but if the sub expects a parameter, it may cause a syntax error because the parentheses are treated as a grouping. Use Call MySub(1) or MySub 1.
Can I call a Sub with named arguments? Yes, you can use named arguments like MySub arg1:=5, arg2:="text" regardless of order. This improves readability.
Is there a limit to how many arguments a Sub can have? VBA supports up to 60 arguments per procedure. Exceeding that causes a compile error.
Best Practices and Safety Note
Always save a copy of your workbook before running any macro, as macros cannot be undone with Ctrl+Z. Use descriptive sub names, declare variables with explicit types, and avoid using On Error Resume Next unless necessary. When calling a Sub that modifies data, ensure you have a backup. Also, remember that macros run with the same permissions as the user, so only run trusted code.
Related
- Power Automate String to HTML: How to Convert & Format
- Power Automate Split Outputs: How To Use
- Balloon Number 7 Foil? Meaning & Real Answer
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA COUNTA: Count Nonblank Cells in MacrosIn Excel VBA, count nonblank cells with Application.WorksheetFunction.CountA and pass it a range. For example, Application.WorksheetFunction.CountA(Worksheets("Data").Range("A2:A100")) returns the…
- Free Macro Recorder for Windows 11: Best OptionsYes. Windows 11 users can record and replay desktop actions for free with Microsoft Power Automate for desktop,…
- Excel VBA: Get a Range’s Length in Cells or CharactersIn Excel VBA, use Range.CountLarge to get how many cells a range contains; use Range.Rows.Count and Range.Columns.Count for…
- Excel Macro for Multiple-Selection Drop-Down ListsTo let a user choose several items from one Excel drop-down, add a VBA Worksheet_Change macro to the…
- Excel VBA Try Catch: Error Handling ExplainedExcel VBA does not have a native Try...Catch statement. To handle runtime errors, use On Error GoTo to…
- Scripting in VBA: What It Is and How to StartScripting in VBA (Visual Basic for Applications) means writing small programs in the programming language built into Microsoft…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.