You can define a custom function in Excel using either the LAMBDA function for simple logic or Visual Basic for Applications (VBA) for complex automation. Defining a custom function allows you to create a reusable formula that performs a specific calculation, reducing the need to rewrite long, complex formulas across multiple cells and ensuring consistency throughout your workbook.
What You'll Need
- A computer with Microsoft Excel installed (Office 365 is required for LAMBDA functions).
- The Developer tab enabled in the Excel Ribbon (required for VBA functions).
- A basic understanding of the mathematical or logical operation you want the function to perform.
- Administrative permissions to save files in the .xlsm (Excel Macro-Enabled Workbook) format if using VBA.
Step-by-Step
There are two primary ways to define a function. The first is the modern LAMBDA method, which stays within the worksheet, and the second is the VBA method, which uses a coding environment. Labels and menu paths may differ slightly depending on your version of Excel or region.
Method 1: Using the LAMBDA Function (No Coding Required)
- Test your logic in a cell: Before defining the function, write the formula in a cell to ensure it works. For example, if you want a function to calculate a 15% tax, write
=A1 * 0.15. - Convert to a LAMBDA: Wrap your formula in the LAMBDA syntax. The syntax is
=LAMBDA([parameter1, parameter2, ...], calculation). Using the tax example, write:=LAMBDA(amount, amount * 0.15). - Verify the LAMBDA: Since you cannot simply press Enter on a LAMBDA in a cell, add the test value in parentheses at the end:
=LAMBDA(amount, amount * 0.15)(100). If it returns 15, the logic is correct. - Open the Name Manager: Go to the Formulas tab in the top ribbon and select Name Manager.
- Create a New Name: Click New. In the Name field, type the name you want for your function (e.g.,
CalculateTax). Note that names cannot contain spaces. - Enter the Formula: In the Refers to box at the bottom, paste your LAMBDA formula:
=LAMBDA(amount, amount * 0.15). Click OK. - Use your function: You can now use
=CalculateTax(100)anywhere in that workbook.
Method 2: Using VBA (For Advanced Logic)
- Enable the Developer Tab: If you don't see the Developer tab, right-click any tab on the ribbon, select Customize the Ribbon, and check the Developer box in the right-hand column.
- Open the Visual Basic Editor: Click the Developer tab and select Visual Basic (or press
Alt + F11). - Insert a Module: In the VBA editor, go to the Insert menu and select Module. This creates a blank page where your custom code will live.
- Write the Function: Start your code with the
Functionkeyword, followed by the name and parameters. For example, to create a function that calculates the area of a circle:Function CircleArea(radius As Double) As DoubleCircleArea = 3.14159 * (radius ^ 2)End Function - Save the Workbook: Close the VBA window. You must save your file as an Excel Macro-Enabled Workbook (.xlsm). If you save it as a standard .xlsx file, the function will be deleted.
- Apply the Function: In any cell, type
=CircleArea(5)and press Enter.
Common Mistakes
One of the most frequent errors is naming a custom function the same as a built-in Excel function. For example, naming a function SUM or AVERAGE will cause conflicts and likely lead to a #NAME? error or unexpected results. Always use a unique prefix or a descriptive name like MySum or Company_Tax_Calc.
Another common mistake occurs with the LAMBDA function when users forget to remove the test parentheses before saving the function in the Name Manager. If you save =LAMBDA(x, x*2)(10), the function will always return 20 regardless of the input you provide in the cell. Ensure the Refers to field contains only the LAMBDA definition.
In VBA, a common error is failing to define the data types for parameters. While VBA allows "Variants" (generic types), explicitly defining a parameter as Double (for decimals) or String (for text) prevents the function from crashing when it encounters an incompatible data type. Forgetting the End Function statement will also prevent the code from compiling, resulting in a syntax error.
Tips
To make your VBA functions more robust, use Option Explicit at the very top of your module. This forces you to declare all variables, which helps catch typos that would otherwise create new, empty variables and lead to incorrect calculations.
When using LAMBDA, keep your functions modular. Instead of creating one massive function that does five different things, create five small functions and nest them. For example, create a GetBasePrice function and a ApplyDiscount function, then call them both within a final CalculateTotal function.
For VBA functions, consider using the Application.Volatile statement if your function depends on cells that change frequently. This tells Excel to recalculate the function whenever any cell in the workbook changes, ensuring your data is always current, though it can slow down very large spreadsheets.
Troubleshooting
If your function returns a #NAME? error, the first step is to check the spelling. Excel cannot find the function name you typed. If you are using a LAMBDA, go back to the Name Manager and ensure the name exactly matches what you are typing in the cell. If you are using VBA, ensure the function is located in a Module and not under ThisWorkbook or a specific Sheet object, as functions in those locations are not globally accessible.
If your VBA function returns #VALUE!, there is likely a data type mismatch. Check if you are passing text into a function that expects a number. You can fix this by adding an If IsNumeric(parameter) Then check inside your VBA code to handle non-numeric inputs gracefully.
If your custom functions disappear after closing and reopening the file, check the file extension. If the file is saved as .xlsx, all VBA macros and custom functions are stripped out upon saving. You must use .xlsm or .xlsb. If you want the function to be available in every single Excel file you open on your computer, you should save the VBA code in the Personal Macro Workbook (PERSONAL.XLSB).
If a LAMBDA function is not appearing for other users, ensure they are using a version of Excel that supports it. LAMBDA is available in Microsoft 365 and Excel for the Web; it is not available in Excel 2019 or 2016. In these cases, the users will see a #NAME? error because their version of Excel doesn't recognize the LAMBDA keyword.
Frequently Asked Questions
Can I share a custom function with other people?
Yes. For LAMBDA functions, the function is stored within the workbook, so anyone who opens the file can use it. For VBA functions, the file must be saved as .xlsm, and the recipient must "Enable Content" when the security warning appears at the top of the screen.
Is there a limit to how many custom functions I can have?
There is no hard numerical limit, but having hundreds of complex VBA functions can slow down the calculation speed of your workbook. LAMBDA functions are generally more efficient for simple calculations.
Can a custom function change the value of another cell?
No. A User Defined Function (UDF) in Excel can only return a value to the cell it is called from. It cannot change the formatting, delete rows, or modify other cells. To perform those actions, you must write a Sub (Macro) instead of a Function.
Do custom functions update automatically?
Yes, they behave like standard Excel functions. When the input cells (arguments) change, the custom function will recalculate automatically unless you have set your workbook to Manual Calculation mode.
Can I use custom functions in Excel for the Web?
LAMBDA functions work perfectly in Excel for the Web. However, VBA functions (macros) do not run in the web browser; they only work in the desktop application of Excel.
How do I delete a custom function?
For LAMBDA, open the Name Manager, select the function name, and click Delete. For VBA, open the Visual Basic Editor, find the function in the module, highlight the code from Function to End Function, and press Delete.