What a function is and why you'd write one

A function in Excel is a pre-built formula that performs a specific calculation or task. Excel comes with hundreds of them already — SUM adds numbers, AVERAGE finds the middle value, CONCATENATE joins text together. When you type =SUM(A1:A10), you are using a function that Excel wrote for you.

Most of the time, the functions Excel provides are enough. But sometimes you need a calculation that doesn't exist in the standard library, or you need to combine several steps into one reusable formula. That is when you write your own function, which Excel calls a User-Defined Function or UDF. It lives in your spreadsheet and works exactly like a built-in function — you type its name, give it the data it needs, and it returns a result.

The catch: writing a function requires learning Visual Basic for Applications, or VBA, which is the programming language built into Excel. If you have never written code before, this will feel unfamiliar at first. But the basics are learnable, and you do not need to understand everything to write something useful.

Key Takeaways

  • User-defined functions live in the Visual Basic editor, which you open by pressing Alt+F11 (Windows) or Fn+Option+F11 (Mac).
  • Every function starts with Sub or Function, has a name you choose, takes inputs called arguments in parentheses, and ends with End Function.
  • The function's result goes into a variable called the return value, which you set by typing the function name followed by an equals sign and the result.
  • Once you save the file as a macro-enabled workbook (.xlsm), your function appears in the formula bar just like SUM or AVERAGE, and you can use it in any cell.
  • If your function does not appear or returns an error, check that the file is saved as .xlsm, that you closed the editor, and that you spelled the function name correctly when you called it.

Opening the Visual Basic editor and creating a new module

To write a function, you need to get into Excel's code editor. On Windows, press Alt+F11. On Mac, press Fn+Option+F11. A new window opens — this is the Visual Basic for Applications editor, or VBE. It looks like a text editor with a menu bar and a panel on the left showing your workbook structure.

On the left side, you will see a tree labeled "Project — VBAProject". Expand it by clicking the plus sign. You will see your workbook name, and under it, "Microsoft Excel Objects" and a list of sheets. Right-click on the workbook name (not a sheet) and select Insert, then Module. A new item called "Module1" appears in the tree. Click on it, and a blank code window opens on the right. This is where you write your function.

If you already have a Module1 from a previous function, you can reuse it or create a new one — it does not matter. Each module can hold multiple functions.

Writing a simple function step by step

Here is a concrete example: imagine you want a function that takes a price and a tax rate, and returns the total including tax. In the blank code window, type this exactly:

Function CalculateTax(price, taxRate) CalculateTax = price + (price * taxRate) End Function

Let's break down what each line does. The first line, Function CalculateTax(price, taxRate), tells Excel you are defining a new function called CalculateTax. The words in parentheses — price and taxRate — are the inputs, called arguments. When someone uses your function, they will provide these two pieces of information.

The second line, CalculateTax = price + (price * taxRate), does the math. It calculates the tax amount (price times taxRate) and adds it to the original price. The result gets stored in a variable with the same name as the function — this is how Excel knows what value to return. The third line, End Function, closes the function definition. Every function must end this way.

Type this code exactly as shown, including the spaces and parentheses. Then press Ctrl+S to save. A dialog box may appear asking what format to save in — choose Excel Macro-Enabled Workbook (.xlsm). This is required; functions do not work in regular .xlsx files.

Testing your function in a cell

Close the Visual Basic editor by clicking the X button or pressing Alt+F11 again. You are back in your spreadsheet. Click on any empty cell and type a formula using your new function. For example, type =CalculateTax(100, 0.08) and press Enter. The cell should show 108, because 100 plus 8% tax equals 108.

If it works, you can now use CalculateTax anywhere in your spreadsheet, just like you would use SUM or AVERAGE. You can reference cells instead of typing numbers: =CalculateTax(A1, B1) will calculate tax on the price in A1 using the rate in B1. You can copy the formula down a column, and it will adjust the cell references automatically.

If you see an error like #NAME? or #VALUE!, the most common causes are: the file was not saved as .xlsm, you misspelled the function name in the cell, or there is a typo in the code itself. Go back to the editor (Alt+F11), check the spelling and syntax, fix any errors, save again, and try the cell formula once more.

Understanding arguments and return values

Arguments are the pieces of information your function needs to do its job. In CalculateTax, we have two: price and taxRate. You can have as many as you need, or none at all. When you call the function in a cell, you provide a value for each argument, separated by commas.

The return value is what the function gives back. In our example, it is the total price including tax. You set the return value by typing the function name, an equals sign, and the result. Only one return value per function — if you need to return multiple pieces of information, you would need to write multiple functions or restructure your approach.

Arguments can be numbers, text, cell references, or even ranges of cells. If you want a function that sums only the cells in a range that meet a certain condition, you would pass the range as one argument and the condition as another. The more complex your calculation, the more arguments you might need — but keep it simple at first.

Common mistakes and how to fix them

The most frequent error is forgetting to save the file as .xlsm. Excel will not recognize your function if the file is in .xlsx format. If you write a function and it does not appear in your cells, close the file, right-click it, select "Rename", and change the extension from .xlsx to .xlsm. Then open it again and try the formula.

Another common mistake is a typo in the function name or arguments. VBA is case-insensitive for function names, so CalculateTax and calculatetax are the same. But if you type =CalculateTax(100, 0.08) in a cell and the function is actually named CalculateTaxRate, Excel will show #NAME? error. Check the exact spelling in the code and in the cell formula.

If your function returns a wrong answer, the math is probably wrong. Add a comment to your code to explain what each line does, then trace through the calculation by hand with sample numbers. For example, if CalculateTax(100, 0.08) should return 108 but returns something else, check whether you are adding or multiplying correctly. The formula price + (price * taxRate) is correct for this example, but if you wrote price * taxRate instead, you would get 8, which is wrong.

When to use a function versus a regular formula

You do not need a function for every calculation. If you are doing something once or twice, a regular formula in a cell is faster. But if you find yourself typing the same multi-step formula over and over, or if you need the same calculation in multiple spreadsheets, a function saves time and reduces errors.

Functions are also useful when a calculation is complex enough that breaking it into steps makes it clearer. Instead of one giant formula that is hard to read, you can write a function with a clear name that explains what it does. Someone reading your spreadsheet will understand CalculateTax(A1, B1) immediately, whereas they might have to puzzle out =A1+(A1*B1).

If you find yourself needing the same function in many different files, you can save it in a template or in a file called Personal.xlsb, which Excel loads automatically. But for most everyday work, keeping the function in the same file as your data is simpler.

Frequently Asked Questions

Can I use a function I wrote in a different spreadsheet?

Only if that spreadsheet is in the same file. Functions live in the workbook where you wrote them. To use the same function in a different file, copy the entire Module from the Visual Basic editor (right-click the module name, select Export, save the .bas file), then import it into the new file (right-click the project, select Import File). Or rewrite the function in the new file — it is usually faster for simple functions.

What if I want my function to return text instead of a number?

It works the same way. For example, Function Greeting(name) would set Greeting = "Hello, " & name to join text together. The & symbol concatenates strings in VBA. The function returns text instead of a number, but the syntax is identical.

Can I delete a function I no longer need?

Yes. Open the Visual Basic editor (Alt+F11), find the function in the module, select all the lines from Function to End Function, and delete them. Save the file. Any cells using that function will show #NAME? error, so remove those formulas first or they will break.

Why does my function work in one file but not another?

The most likely reason is that the new file is not saved as .xlsm. Functions require macro-enabled format. Also check that you actually copied the function code to the new file — just opening both files at once does not share functions between them.

Can I use Excel's built-in functions inside my own function?

Yes, absolutely. You can use SUM, AVERAGE, IF, and any other built-in function inside your custom function. For example, Function AverageTax(priceRange, taxRate) could use =AVERAGE(priceRange) to find the average price, then multiply by taxRate. This is one of the most useful ways to combine existing functions into something new.