Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA Public Function: How to Create and Use One

· 7 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

A public function in Excel VBA is a custom function you write in a standard module and declare with the Public keyword so that it can be called from any other module in the same workbook, and — if the workbook is saved as an add-in or macro-enabled file and the function is written correctly — from worksheet cells like a built-in formula. The shortest working recipe is: open the VBA editor with Alt+F11, insert a standard module, type Public Function MyFunc(x As Double) As Double followed by your calculation and End Function, then call it from a cell as =MyFunc(A1) after saving the file as .xlsm. The rest of this guide covers the details that make the difference between a function that works everywhere and one that returns #NAME? or refuses to appear in the formula list.

What You'll Need

  • Excel on Windows or Mac with the Developer tab enabled, or the ability to press Alt+F11 (Windows) or Option+F11 (Mac) to open the Visual Basic Editor.
  • A workbook saved as Excel Macro-Enabled Workbook (*.xlsm) or Excel Binary Workbook (*.xlsb). A plain .xlsx file cannot store VBA code, and saving as .xlsx will strip it.
  • Macro security set to allow macros. In the Trust Center, the setting is under File > Options > Trust Center > Trust Center Settings > Macro Settings on Windows; on Mac it is under the Excel menu > Preferences > Security. Labels vary slightly by version and platform.
  • A clear idea of what the function should return and what inputs it needs. VBA functions can take zero or more arguments and must return a value.

Step-by-Step

  1. Save the workbook in a macro-enabled format first. Go to File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm). Doing this before you write code avoids the surprise of losing your function when Excel prompts you to save as .xlsx later.
  2. Open the Visual Basic Editor. Press Alt+F11 on Windows or Option+F11 on Mac. If nothing happens, enable the Developer tab via File > Options > Customize Ribbon and tick Developer, then click Visual Basic on that tab.
  3. Insert a standard module. In the editor, use Insert > Module. A standard module is where public functions belong. Do not put a worksheet function in a sheet's code module (the ones named Sheet1, Sheet2, ThisWorkbook) if you want it callable from other modules and from cells — code there is private to that sheet by default.
  4. Write the function with the Public keyword. Type something like:
    Public Function SalesTax(amount As Double, rate As Double) As Double
        SalesTax = amount * rate
    End Function
    The Public keyword makes it visible outside the module. The As Double after the closing parenthesis declares the return type. Assign the result to the function's own name — that is how VBA returns a value.
  5. Use explicit argument types. Declare each parameter with a type (As Double, As String, As Range, As Variant). Untyped arguments default to Variant, which is slower and hides errors. If a function should accept a whole range, declare it As Range and handle the case where the range has more than one cell.
  6. Return a value on every path. If you use If/Then branches, make sure each branch assigns to the function name. A function that falls through without assigning returns the default for its type — 0 for numeric types, an empty string for String — which looks like a wrong answer rather than an error.
  7. Test it from the Immediate Window. Press Ctrl+G in the editor to open the Immediate Window and type ?SalesTax(100, 0.2), then press Enter. You should see 20. This confirms the function compiles and calculates before you involve worksheet cells.
  8. Call it from a worksheet cell. Return to Excel and enter =SalesTax(A1, B1) in a cell. A correctly declared public function in a standard module of a macro-enabled workbook appears in the formula autocomplete list as you type. If it does not appear, see the troubleshooting section.
  9. Save and, if needed, distribute as an add-in. To make the function available in every workbook, save the file as an Excel Add-In (*.xlam) via Save As, then enable it through File > Options > Add-ins > Excel Add-ins > Go. Note that an .xlam must be enabled on each machine that needs the function.

Common Mistakes

  • Declaring the function Private by accident. If you omit the keyword, VBA treats a Function as Public by default in a standard module — but if you write Private Function or place the code in a sheet module, it will not be callable from other modules or from cells.
  • Saving as .xlsx. Excel warns you that VBA will be removed; if you accept, the function disappears. Always keep an .xlsm or .xlsb copy.
  • Naming the function the same as a cell reference or a built-in function. A function called Sum or AB1 will confuse Excel. Use a distinctive name such as MySum or CalcTax.
  • Forgetting to assign the return value. Writing SalesTax = amount * rate is required; just calculating amount * rate on its own line does nothing.
  • Using worksheet-only objects inside the function. Functions called from cells cannot change other cells, select ranges, or open dialogs. If your function tries to do that, Excel returns #VALUE!.
  • Putting the code in ThisWorkbook or a sheet module. Those modules are for event code; public reusable functions belong in a standard module.

Tips

  • Add Option Explicit at the top of the module. It forces you to declare every variable and catches typos that would otherwise silently create new variables.
  • Keep functions pure: take inputs, return a value, and avoid reading or writing cells inside the function. This makes them faster and predictable when recalculated.
  • Use Application.Volatile only if the function depends on something Excel cannot see, such as the current time. Volatile functions recalculate on every change and can slow a large workbook.
  • Document arguments with a short comment above the function so other users know what to pass in.
  • If you need the function in many workbooks, an .xlam add-in is cleaner than copying code into each file.

Troubleshooting

If the function returns #NAME?, Excel cannot find it. Check that the code is in a standard module, that the keyword is Public (or omitted), that the workbook is macro-enabled, and that macros are enabled. If the function does not appear in autocomplete, close and reopen the workbook — the function list refreshes on load.

If you see #VALUE!, an argument is the wrong type or the function tried to do something a worksheet function cannot do, such as changing another cell. Simplify the function to a single calculation and add arguments back one at a time.

If the function works in the Immediate Window but not in a cell, the most likely cause is that it lives in a sheet module or is declared Private. Move it to a standard module and remove any Private keyword.

If the code stops running entirely, check Trust Center macro settings and whether the file was saved as .xlsx. Re-save as .xlsm and re-enable macros. If the workbook came from another source, Windows may have blocked it — right-click the file, open Properties, and look for an Unblock option before opening.

Frequently Asked Questions

What is the difference between a Public and a Private function in VBA?

A Public function can be called from other modules and, if it is in a standard module of a macro-enabled workbook, from worksheet cells. A Private function is visible only inside the module where it is written.

Can a public VBA function be used in every workbook automatically?

Not automatically. Save the workbook as an Excel Add-In (.xlam) and enable it through the Add-ins dialog on each machine. Otherwise the function is available only in the workbook that contains the code.

Why does my function not show up in the formula list?

The usual reasons are that the code is in a sheet module, the function is Private, the file is not macro-enabled, or macros are disabled. Fix those and reopen the workbook.

Can a public function change a cell's formatting or value?

No. Functions called from worksheet cells must return a value and cannot modify the worksheet. Use a Sub procedure for actions that change cells.

Does a public function work on Excel for Mac?

Yes, VBA and public functions work in Excel for Mac, though the keyboard shortcut to open the editor is Option+F11 and some Trust Center labels differ. Save as .xlsm the same way.

How do I pass a whole range to a public function?

Declare the argument As Range and decide inside the function whether to handle a single cell or many. For multi-cell ranges, loop over the cells or use a Variant array to read the values.