To call a function in VBA, you either assign its return value to a variable (e.g., result = MyFunction(arg1, arg2)) or, if the function returns a value you want to ignore, call it as a standalone statement (e.g., MyFunction arg1, arg2). The syntax differs slightly depending on whether you’re calling from another procedure, from a worksheet cell, or from the Immediate window.
What Is a VBA Function vs. a Sub?
VBA has two types of procedures: Sub and Function. A Sub performs an action but does not return a value. A Function performs an action and returns a value, which can be used in an expression or assigned to a variable. Functions are essential for reusable calculations, string manipulation, and logic that you want to call from multiple places in your code.
For example, a function that calculates the area of a circle might look like this:
Function CircleArea(radius As Double) As Double
CircleArea = 3.14159 * radius ^ 2
End Function
This function can be called from a Sub, from another function, or even directly from a worksheet cell (if it’s placed in a standard module).
How to Call a Function from a Sub or Another Function
When calling a function from within another procedure, you have two main options:
- Use the return value – assign the result to a variable:
Sub ShowArea()
Dim r As Double
r = CircleArea(5)
MsgBox "Area is " & r
End Sub
- Ignore the return value – call it as a statement, but you must omit the parentheses and the
Callkeyword is optional (though usingCallwith parentheses is allowed):
Sub CallWithoutUsing()
' Parentheses not allowed if ignoring return value without Call
CircleArea 5 ' This runs the function, but the result is discarded
End Sub
If you use Call, you must include parentheses: Call CircleArea(5). However, this still discards the return value. In practice, you usually want the result, so the first method is more common.
Calling a Function from a Worksheet Cell
If your function is placed in a standard module (not a class module or a worksheet module), you can call it directly from a cell in Excel, just like built-in functions. For example, with the CircleArea function above, in cell A1 you could type:
=CircleArea(3)
This would return 28.27431 (using the approximation of pi in the function). Note that the function must be Public (which is the default) and must be in a standard module. If it’s in a worksheet module, Excel won’t recognize it in a cell.
Calling a Function from the Immediate Window
For quick testing, you can call a function directly in the Immediate Window (press Ctrl+G in the VBA editor). To print the result, use Debug.Print or just type ?FunctionName(argument). For example:
?CircleArea(5)
This prints the result to the Immediate Window. This is handy for debugging small pieces of logic without writing a whole Sub.
Passing Arguments: ByRef vs. ByVal
When you call a function, you pass arguments either by reference (ByRef, the default) or by value (ByVal). ByRef means the function can modify the original variable; ByVal passes a copy, so the original is unchanged. This matters when you call a function that changes its parameters.
Example:
Function AddOne(ByVal x As Integer) As Integer
x = x + 1
AddOne = x
End Function
Sub TestByVal()
Dim num As Integer
num = 5
Dim result As Integer
result = AddOne(num)
' num is still 5 because ByVal passed a copy
End Sub
If you had used ByRef, num would become 6 after the call. Understanding this helps avoid unexpected side effects.
Common Errors When Calling Functions
- Wrong number of arguments – VBA expects exactly the parameters defined. If you pass fewer or more, you get a compile error.
- Type mismatch – If you pass a string where a number is expected, you get a runtime error 13.
- Parentheses misuse – When calling a function as a statement (ignoring return value), using parentheses without
Callcauses a syntax error. Always useCallor omit parentheses. - Function not in a standard module – If you try to call a function from a cell and it’s in a worksheet module, it won’t work.
Frequently Asked Questions
Can I call a function without using its return value?
Yes. Just write the function name with arguments, without parentheses, as a standalone statement. For example: MyFunction 10, 20. The function executes, but the result is discarded. This is useful for functions that have side effects (like modifying a global variable), though such designs are often discouraged.
What’s the difference between Call and just typing the function name?
Call is optional for Subs and functions when you omit parentheses. Using Call forces you to use parentheses, but it’s mainly a stylistic choice. For functions, if you use Call, you cannot capture the return value. Many programmers avoid Call entirely.
Why does my function return #VALUE! in Excel?
This typically happens when the function encounters a type mismatch or an error during calculation. For example, if you pass text that can’t be converted to a number, or if the function uses a worksheet function that fails. Check the function’s logic and ensure the arguments are valid.
Can I call a function from another workbook?
Yes, if the function is in a standard module and the workbook is open, you can reference it like =WorkbookName!FunctionName(args) in a cell. In VBA, you can use Application.Run to call a function from another workbook, but it’s often simpler to set a reference to the workbook’s VBA project (Tools > References).
Is there a limit to how many arguments a function can take?
VBA functions can take up to 60 arguments in older versions, but in modern Excel VBA (365), the limit is 255. In practice, you rarely hit that limit. If you need more, consider using an array or a custom object.
Best Practices for Writing and Calling Functions
- Always declare the return type (e.g.,
As Double) to avoid variant overhead and improve performance. - Use meaningful names for functions and parameters.
- Keep functions pure (no side effects) when possible – they should only return a value based on inputs.
- If a function modifies its arguments, document that clearly or use
ByValto prevent it. - Test functions in the Immediate Window before using them in larger code.
Remember to save a copy of your workbook before running any macro that calls functions, because macros cannot be undone with Ctrl+Z.
Related
- Random Number in JS: Math.random() Explained
- Shut Off Computer Shortcut: All Methods
- Data Entry With No Experience? Yes, Here’s How
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Pivot Table Not Refreshing Through VBA? Causes and FixesIf an Excel PivotTable does not update through VBA, first confirm that its source includes the changed rows,…
- VBA Macro to Copy and Paste a Row Below the Last RowTo copy the last data row into the row immediately below it, find the last occupied cell in…
- Excel VBA AutoFilter Off: Clear Filters or Remove ArrowsTo turn off a standard worksheet AutoFilter in Excel VBA, first clear any active filter so the filtered-out…
- Excel VBA AutoFill: Methods, Examples, and FixesIn Excel VBA, use the Range.AutoFill method to extend a formula, value pattern, or formatting into a destination…
- Excel VBA: Add a New Worksheet With a Specific NameIn Excel VBA, add a worksheet with Worksheets.Add, then set its Name property. For example, the key steps…
- Remove VBA From an Excel Workbook SafelyTo remove VBA from an Excel workbook, open it in the desktop version of Excel, choose File >…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.