Microsoft Excel: How to Create Your Own Function
Sometimes, the specific requirements of a project in Excel can't be met with an existing formula. We'll show you how to build your own formula for this!
- Published
- Content reviewed
Listen to this article
Start reading aloud with a voice from your device. Changing the voice, speed or section stops playback; you can then start again.
Checking available voices.
Overview
Microsoft, the maker of Windows 10—the world's most widely used operating system (which has replaced its predecessor, Windows 7)—is known not only for producing operating systems for computers but also, among other things, for the highly successful Microsoft Office suite.
While almost everyone uses Outlook, Word, and PowerPoint and is familiar with most of their features, Excel tends to be overlooked on many computers. This is completely unjustified, because Excel is an incredibly useful program. Even with just the most basic knowledge of Excel, you can complete many tasks with just a few clicks or keystrokes.
Anyone who knows even the most basic Excel functions can quickly and easily figure things out on their own. But it often happens that you want to perform the same task in Excel over and over again, for which there’s no built-in function. In that case, you can either go through the trouble of manually adjusting and calculating—or you can create your own function. It’s actually pretty easy with our tutorial.
Creating a function is similar to creating a macro
To create a new function in Excel, start by following a process similar to the one you'd use to create an Excel macro.
To do this, open a blank worksheet in Excel and start recording a macro. To create a macro, you must enable the "Show Developer Tools" tab.
You can find this option under the "Start" tab and "Options." In the "Options" window, go to the "Customize Ribbon" section and check the box next to "Developer Tools."
Now a small square also appears in the lower-left corner of the Excel window. Clicking on it allows you to record a macro.
Instead of recording a macro, simply stop the recording right after you've named the macro. Next, go to the "Developer Tools" tab and open the Macro Editor.
To open it, click “Macro” and then “Edit” in the drop-down menu. A new window will open with the complex name “Microsoft Visual Basic for Applications.” Here, you can use the VBA programming language to program automations, for example, but you can also easily create functions—even without any programming knowledge.
Here's how to program your function
In this window, you can now see the code for the macro you just programmed, which has no function whatsoever.
You'll need to make a few improvements to your own function here. First, delete everything between the first and last lines. Then replace the word "Sub " with the word "Function ".
Then replace the name of the macro with the name of the function. To calculate the net price of a product or service, you could name the function NETPRICE. To let Excel know that it should take the value from a cell, the variable "a" is placed in parentheses after NETTOPREIS. If you want to include more cells in a function, you would then add a comma followed by the variable "b," and so on.
So then you type the following into the editor:
Function NET PRICE (a)
The editor should now look like this:
To let Excel know which calculation it should perform, describe the calculation between the lines “Function” and “End Function.”
The calculation method for Excel is to use the NETPRICE function. And the net price is variable A divided by 119, multiplied by 100.
So you need to enter the following line in the editor:
NET PRICE=a/119100
If the function is to perform additional mathematical steps, they are simply written after the equals sign in accordance with proper mathematical notation.
You can save the function by clicking on the floppy disk icon. If you selected .xslx as the file format, you must save the file as .xslm; otherwise, the file will not contain any macros.
Now you can test whether your function works the way you want it to.
About the authors
Wiresoft Editorial Team
The Wiresoft editorial team explains software, licenses, and how to use Windows, Office, and servers in a clear and practical way. The articles are based on questions that customers ask Wiresoft in their day-to-day lives.
Sources and further reading
- Microsoft Support: Help and Learning for ExcelSource date:
Matching productsOffice