Custom functions in Excel let you extend Excel’s built-in capabilities by writing your own formulas using VBA (Visual Basic for Applications). This is useful when you need to perform a calculation that isn’t covered by existing functions like SUM, VLOOKUP, or IF — for example, calculating tax based on a complex bracket system, converting units with custom logic, or extracting specific text patterns. You don’t need to be a programmer, but you must enable the Developer tab and have permission to run macros. This guide works for Excel on Windows (desktop) and Excel for Microsoft 365. It does not work on Excel for iOS, Android, or the web version.
What You'll Need
- Excel installed on a Windows PC (desktop version, not web or mobile)
- The Developer tab enabled in Excel
- Permission to enable macros in the workbook
- A clear idea of what your custom function should calculate
Step-by-Step
- Open Excel and create a new blank workbook or open an existing one where you want to use the function.
- Go to the File menu, then select Options. In the Excel Options window, click Customize Ribbon on the left.
- In the right pane, under Main Tabs, check the box labeled Developer. Click OK to save.
- Click the Developer tab on the Excel ribbon. In the Code group, click Visual Basic. This opens the Visual Basic for Applications editor.
- In the Project Explorer window on the left (if not visible, press Ctrl+R), right-click your workbook name (like VBAProject (YourWorkbookName.xlsx)) and choose Insert > Module. A new blank code window will appear.
- Type your function code. For example, to create a function called
DoubleValuethat multiplies a number by 2, write:Function DoubleValue(inputNumber As Double) As Double DoubleValue = inputNumber * 2 End FunctionReplace the function name and logic with your own. Always define input types (likeAs Double) and return type. - Save the workbook as a Macro-Enabled Workbook (.xlsm). Go to File > Save As, choose Excel Macro-Enabled Workbook (*.xlsm) from the Save as type dropdown, then save.
- Close the VBA editor and return to your Excel sheet.
- Use your new function like any built-in function. In any cell, type
=DoubleValue(5)and press Enter. It will return 10. You can also reference a cell:=DoubleValue(A1).
Common Mistakes
- Trying to use the custom function in Excel Online, Excel for iPad, or mobile — these versions do not support VBA macros.
- Forgetting to save as .xlsm. If saved as .xlsx, macros are stripped and the function disappears.
- Typing the function name incorrectly — VBA is case-insensitive, but Excel formulas must match the exact spelling you defined.
- Using unsupported data types. For example, if your function expects a number but you pass text, it returns #VALUE!. Always validate input with
If Not IsNumeric(input) Thenif needed. - Not enabling macros when opening the file. Excel blocks macros by default. You must click Enable Content when prompted.
Tips
- Test your function with simple values first. If
=DoubleValue(3)doesn’t return 6, check your code for typos. - Use meaningful names. Avoid names that match Excel’s built-in functions (like
SUMorMAX), even if spelled differently. - Include comments in your VBA code with an apostrophe (
') to explain what each part does — it helps you or others maintain it later. - For functions that return text, use
As Stringinstead ofAs Double. For example:Function GetFirstWord(text As String) As String. - Use the Function Wizard (fx button) to insert your custom function. It will show your function under the User Defined category.
- Keep your custom functions in a single module per workbook. Avoid cluttering with too many modules.
Troubleshooting
If your custom function doesn’t appear or returns an error:
- Check if macros are enabled. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings. Ensure "Enable all macros" or "Disable all macros with notification" is selected. Restart Excel after changing.
- Confirm you saved the file as .xlsm. Open File Explorer, find your file, and check the extension. If it says .xlsx, you lost the function.
- Reopen the VBA editor (Developer > Visual Basic) and check if your function code is still there. If it’s missing, you may have edited the wrong module or saved incorrectly.
- Look for syntax errors in VBA. Red text in the code window means there’s a mistake — fix it before closing the editor.
- If the function returns #NAME?, Excel doesn’t recognize it. This means the macro is blocked, the workbook isn’t trusted, or the function name is misspelled in the formula.
- Restart Excel. Sometimes the VBA environment doesn’t reload properly after editing.
- If you’re on a work computer, your IT department may block macro execution. Contact your system administrator for permission.
Frequently Asked Questions
Can I use a custom function across multiple workbooks?
No, each workbook must contain its own copy of the VBA code. To reuse functions, save the workbook as an add-in (.xlam) and install it via File > Options > Add-Ins > Manage Excel Add-ins.
Do custom functions work with Excel tables or structured references?
Yes, but you must pass the cell or range as an argument. For example, if you use =DoubleValue(Table1[Sales]), the function receives an array. You’ll need to modify your VBA code to handle arrays if you want to process multiple values at once.
Can I create functions that change cell formatting or insert rows?
No. Custom functions (UDFs) can only return a value to a cell. They cannot change formatting, delete rows, or alter other cells. For those actions, use a macro (Sub procedure) triggered by a button or event.
Why does my function show #VALUE! even when I enter a number?
This usually means your function expects one data type but received another. For example, if your function is declared as As Double but you pass text like "hello", Excel can’t convert it. Check your input and use IsNumeric() in VBA to validate.
Is there a limit to how complex a custom function can be?
Functionally, no — you can write loops, conditionals, and call other functions. But performance suffers. Avoid heavy loops or reading from external sources like web pages. Keep functions fast to prevent Excel from freezing.
Can I share a workbook with custom functions with someone who doesn’t have Excel?
No. Only Excel for Windows or Mac can run VBA macros. If shared with someone using Excel Online, Google Sheets, or another tool, the function will not work and will show as #NAME? or blank.