VBA Excel built-in functions let you perform calculations, text manipulation, date handling and lookups directly in your macros, using the same functions you already know from worksheet formulas. To use one, call it with Application.WorksheetFunction (for worksheet functions) or directly (for VBA's own functions like MsgBox or Left). This guide shows you how, step by step, with practical examples and fixes for common errors.
What You'll Need
- Excel installed on your computer (any modern version).
- Basic familiarity with the VBA editor: how to open it (Alt+F11) and insert a module.
- Macro-enabled workbook (.xlsm) if you want to save your code.
Step-by-Step
- Open the VBA editor. In Excel, press Alt+F11 (on Mac, Fn+Alt+F11 or use the Developer tab). If the Developer tab is not visible, enable it via File > Options > Customize Ribbon and tick Developer.
- Insert a module. In the VBA editor, go to Insert > Module. This is where you will write your code.
- Understand the two categories. VBA has its own functions (e.g.,
Left,MsgBox,Date) and worksheet functions (e.g.,VLookup,Sum,Text). Worksheet functions are accessed throughApplication.WorksheetFunction. - Write a simple VBA function call. Type
Sub Demo()thenMsgBox Left("Hello World", 5)andEnd Sub. Press F5 to run. A message box shows "Hello". - Call a worksheet function. Add a new line:
MsgBox Application.WorksheetFunction.Sum(Range("A1:A10")). This sums the values in A1:A10. Run it. - Use named arguments for clarity. For functions with many arguments, like
VLookup, you can useApplication.WorksheetFunction.VLookup(lookup_value, table_array, col_index_num, range_lookup). Example:MsgBox Application.WorksheetFunction.VLookup("Apple", Range("A1:B10"), 2, False). - Handle errors with On Error. Worksheet functions raise errors if inputs are invalid. Wrap calls in
On Error Resume Nextand checkErr.Number, or useOn Error GoTolabels. Reset withOn Error GoTo 0. - Use the Object Browser to explore. Press F2 in the VBA editor, type
WorksheetFunctionin the search box, and browse the list of available functions. This helps you discover what is available. - Test with the Immediate Window. Press Ctrl+G to open the Immediate Window. Type
?Application.WorksheetFunction.Max(1,5,3)and press Enter to see the result instantly. - Save your work. Save the workbook as .xlsm to keep macros. Use File > Save As and choose Excel Macro-Enabled Workbook.
Common Mistakes
- Forgetting the WorksheetFunction prefix. Writing
VLookup(...)directly causes a "Sub or Function not defined" error. UseApplication.WorksheetFunction.VLookup. - Mixing up VBA and worksheet function names. For example, VBA's
Lenworks directly, but worksheet'sLENis not a VBA function. UseLenwithout prefix. - Not handling errors. Worksheet functions like
VLookupthrow errors if no match is found. Without error handling, your macro stops. - Using wrong argument types. Passing a string where a number is expected causes a "Type mismatch" error. Check the expected types in the Object Browser.
- Assuming all worksheet functions are available. Some newer functions (e.g., XLOOKUP) may not be exposed in older Excel versions' VBA. Test on your version.
Tips
- Use the macro recorder. Record a macro that uses a worksheet function, then inspect the code to see the exact syntax.
- Prefer VBA functions for simple tasks. For string manipulation, VBA's
Left,Right,Midare faster and simpler than worksheet equivalents. - Use
Application.WorksheetFunctionfor complex calculations. Functions likeSumIf,CountIf, andIndexsave you writing loops. - Check for errors with
IsError. When calling a worksheet function, you can test the result withIsErrorto avoid runtime errors. - Use
Evaluatefor quick formulas.Evaluate("SUM(A1:A10)")works but is slower and less safe thanWorksheetFunction.
Troubleshooting
- "Sub or Function not defined": You likely omitted
Application.WorksheetFunctionor misspelled the function name. Check spelling and prefix. - "Unable to get the VLookup property of the WorksheetFunction class": This means VLookup failed (no match). Wrap in error handling or use
Application.VLookupwhich returns an error value instead of raising an error. - "Type mismatch": An argument has the wrong data type. For example, passing text to a function expecting a number. Convert with
CLng,CDbl, orCStr. - Macro runs but gives wrong result: Check that ranges are correct and that you are not including headers when the function expects data only. Use
Debug.Printto output intermediate values. - Function not available: Some worksheet functions are not exposed in VBA. Use
Evaluateas a fallback, or implement the logic manually.
Frequently Asked Questions
Can I use any Excel worksheet function in VBA?
Most, but not all. Functions like VLookup, Sum, Text are available via WorksheetFunction. Newer dynamic array functions may not be exposed. Check with the Object Browser.
What is the difference between Application.WorksheetFunction and Application?
Application.WorksheetFunction gives you access to worksheet functions and raises errors on failure. Using Application.VLookup (without WorksheetFunction) returns an error value instead of raising an error, which can be easier to handle.
How do I use VLookup in VBA?
Use Application.WorksheetFunction.VLookup(lookup_value, table_array, col_index_num, range_lookup). For example: result = Application.WorksheetFunction.VLookup("Apple", Range("A1:B10"), 2, False).
Why does my macro stop with an error when using VLookup?
If no match is found, WorksheetFunction.VLookup raises an error. Use On Error Resume Next or switch to Application.VLookup and check with IsError.
Can I use VBA functions like Left without WorksheetFunction?
Yes. VBA has its own set of functions (Left, Right, Mid, Len, etc.) that you call directly without any prefix.
How do I find the correct syntax for a worksheet function?
Use the Object Browser (F2) and search for WorksheetFunction. Select a function to see its arguments. Alternatively, record a macro that uses the function and inspect the generated code.