You call Excel's built-in worksheet functions from VBA through the WorksheetFunction object, like this: Range("A1").Value = Application.WorksheetFunction.Sum(Range("B1:B10")). That single line runs the same SUM Excel uses on the sheet, but from inside your macro. The catch is that WorksheetFunction is strict: if the function would return an error on the sheet, your macro stops with a runtime error instead of quietly showing #N/A. So the practical approach is to call the function, then wrap it in error handling or check your inputs first.

This guide covers how to call worksheet functions from VBA, how to pass ranges and arguments correctly, how to handle the errors they throw, and the cases where a native VBA approach is safer. It applies to Excel for Windows and Excel for Mac; the VBA editor and the WorksheetFunction object exist in both, though a handful of functions are Windows-only or Mac-only, and menu labels can differ slightly by version and region.

What You'll Need

  • Excel with macros enabled (the desktop app, not Excel for the web, which has no VBA editor).
  • Access to the VBA editor: press Alt+F11 on Windows, or Option+F11 on Mac.
  • A workbook saved as .xlsm (macro-enabled) if you want to keep the code.
  • Macro security set to allow macros in your own files; check File > Options > Trust Center > Trust Center Settings > Macro Settings, or the equivalent Excel > Preferences path on Mac.
  • Basic familiarity with variables, ranges and If statements in VBA.

Step-by-Step

  1. Open the VBA editor and insert a module. Press Alt+F11 (Option+F11 on Mac). In the Project pane, right-click your workbook, choose Insert > Module. Code in a standard module can be run from the Macros dialog; code in a sheet module only runs on that sheet's events.
  2. Write a Sub and declare your variables. For example: Sub SumDemo() then Dim total As Double and Dim rng As Range. Declaring types up front makes errors easier to spot than letting VBA guess with Variant.
  3. Point a Range variable at your data. Use Set rng = ThisWorkbook.Worksheets("Sheet1").Range("B2:B100"). Qualify the workbook and worksheet explicitly. Unqualified Range("B2:B100") refers to whatever sheet is active, which is the most common source of wrong results in macros.
  4. Call the function through Application.WorksheetFunction. Type total = Application.WorksheetFunction.Sum(rng). After the dot, the editor's IntelliSense list shows the available functions; typing Application.WorksheetFunction. and then a few letters is the reliable way to find the exact name.
  5. Match the argument order to the sheet function. Worksheet functions keep their sheet argument order. VLookup(lookup_value, table_array, col_index_num, range_lookup) takes the same four arguments in the same order as the sheet formula. For optional arguments you can omit them, but you cannot skip a middle argument without using a named argument or a placeholder.
  6. Pass ranges, not strings, where a range is expected. Sum(Range("A1:A10")) works; Sum("A1:A10") does not, because the function expects a Range object or an array of values. If you have text like "A1:A10", convert it with Range(addressString) first.
  7. Handle the error case. Wrap the call so a bad lookup does not kill the macro:
    On Error Resume Next
    result = Application.WorksheetFunction.VLookup(key, tbl, 2, False)
    If Err.Number <> 0 Then
      result = "Not found"
      Err.Clear
    End If
    On Error GoTo 0
    Reset error handling with On Error GoTo 0 as soon as the risky call is done, so later bugs are not silently swallowed.
  8. Write the result back to a cell. Worksheets("Sheet1").Range("D1").Value = total. Use .Value for the result, not .Formula, unless you deliberately want a formula string written into the cell.
  9. Run and test. Press F5 with the cursor inside the Sub, or use Run > Run Sub/UserForm. Test with an empty range, a range containing text, and a range containing an error value such as #DIV/0!, because those are the three inputs that most often trip WorksheetFunction.
  10. Save as .xlsm. Use File > Save As and choose Excel Macro-Enabled Workbook. A plain .xlsx save strips the code.

Common Mistakes

  • Assuming WorksheetFunction returns an error value. It does not. On the sheet, VLOOKUP returns #N/A; through WorksheetFunction, the same miss raises runtime error 1004. You must catch it.
  • Leaving ranges unqualified. Code that works while Sheet1 is active breaks when the user is on another sheet. Always name the workbook and worksheet.
  • Passing a whole column. Range("A:A") into a worksheet function can be slow on large workbooks. Bound the range to the used rows.
  • Using WorksheetFunction for things VBA already does. Counting rows, trimming text or checking whether a cell is empty are faster and clearer with native VBA (for example Len, Trim, IsEmpty) than with a worksheet call.
  • Forgetting that some functions are version- or platform-specific. A function available on Windows Excel may be missing on Mac, and newer functions may not exist in older versions. If the editor does not list it, it is not available in that build.
  • Swallowing every error with On Error Resume Next left on. Leaving it active for the whole procedure hides real bugs and produces blank or zero results with no explanation.

Tips

  • Use the IntelliSense list after Application.WorksheetFunction. to confirm a function name rather than typing from memory.
  • For a single value you can also use Evaluate with a formula string, but WorksheetFunction is clearer and easier to debug; prefer it.
  • If you need the error value rather than a runtime error, call the function through Application without WorksheetFunction (for example Application.VLookup(...)), which returns an error Variant you can test with IsError. This is the standard alternative when a miss is expected.
  • Keep one small helper function per calculation so the error handling lives in one place instead of being repeated in every Sub.
  • Comment the argument order next to each call; six months later the fourth argument of a lookup is easy to get wrong.

Troubleshooting

Runtime error 1004 "Unable to get the ... property of the WorksheetFunction class". The function could not produce a result: no match, a division by zero, or text where a number was expected. Check the inputs, then add error handling around the call.

Runtime error 438 "Object doesn't support this property or method". Usually a misspelled function name or a function not present in your Excel version or platform. Re-enter it from the IntelliSense list.

Wrong totals or values. Check that the range is qualified to the right sheet and that it does not include header text. Print the range address with Debug.Print rng.Address and read it in the Immediate window (Ctrl+G).

Macro runs but nothing happens. Confirm the Sub is in a standard module, that macros are enabled, and that you are writing to the intended sheet. Step through with F8 to watch each line.

Code works on your machine, fails on a colleague's. Check Excel version and platform. Replace any function that is not available in their build with a native VBA equivalent.

If that did not work, copy the failing call into a fresh one-line Sub with hardcoded values. If it works there, the problem is in how the range or arguments are built, not in the function itself.

Frequently Asked Questions

What is the difference between Application.WorksheetFunction.Sum and Application.Sum?

The first raises a runtime error when the calculation fails; the second returns an error value you can test with IsError. Use WorksheetFunction when you want a clean number or an immediate stop, and the Application form when a failure is expected.

Can I use any Excel function from VBA?

Most built-in worksheet functions are exposed, but not all, and availability varies by Excel version and platform. If a name does not appear in the IntelliSense list, it is not callable that way in your build.

Why does my VLOOKUP call fail when the same formula works on the sheet?

On the sheet a miss shows #N/A; in VBA it becomes error 1004. Wrap the call in error handling, or use Application.VLookup and check IsError on the result.

Do worksheet function calls make my macro slower?

Each call crosses from VBA into Excel's calculation engine, so thousands of calls in a loop add up. Read the range into a VBA array once and loop over the array when performance matters.

Does this work in Excel for the web?

No. Excel for the web has no VBA editor. You need the desktop app on Windows or Mac to write and run this code.

Can I pass a named range?

Yes. Range("MyNamedRange") works as the argument, provided the name exists in the workbook the code refers to.