Microsoft Excel: How to create your own function
Sometimes, the specific requirements of a project in Excel cannot be met using an existing formula. We’ll show you how to create 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, as the manufacturer of Windows 10 – the world’s most widely used operating system (which has thus replaced its predecessor, Windows 7) – is, as is well known, not only a producer of computer operating systems, but also, amongst other things, the highly successful Microsoft Office suite.
Whilst almost everyone uses Outlook, Word and PowerPoint and is familiar with most of their functions, Excel tends to lead a rather obscure existence on many computers. This is completely unjustified, as Excel is an incredibly useful programme. Even with just the most basic knowledge of Excel, you can carry out many tasks with just a few clicks or keystrokes.
Anyone who is already familiar with even the most basic functions in Excel can sort things out quickly and easily. But it’s often the case that you want to carry out the same task in Excel time and time again, for which there’s no ready-made function. In that case, you can either go to the trouble of making manual adjustments and doing the calculations yourself – or you can create your own function. It’s actually quite simple, with our tutorial.
Creating a function is similar to creating a macro
To create a new function in Exceleine, you start off in much the same way as you would when creating an Excel macro.
To do this, open a blank worksheet in Excel and start recording a macro. To create a macro, you’ll need to enable the ‘Show Developer Tools’ tab.
You can find this option under the ‘Start’ tab and ‘Options’. In the ‘Options’ window, go to the ‘Customise Ribbon’ section and tick the box on the right-hand side next to ‘Developer Tools’.
A small square now also appears at the very bottom left of the Excel window. By clicking on it, you can record a macro.
Instead of recording a macro, simply stop the recording straight away after you’ve given the macro a name. Next, go to the ‘Developer Tools’ tab and open the macro editor.
To open this, click on ‘Macro’ and then select ‘Edit’ from the drop-down menu. A new window will now open with the rather complicated name ‘Microsoft Visual Basic for Applications’. Here, you can use the VBA programming language to programme automations, for example, but you can also create functions very easily – without any programming knowledge.
How to programme your function
In this window, you can now see the code for the macro you have 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 you replace the name of the macro with the name of the function. To calculate the net price of a product or service, you could call the function NETPRICE. To let Excel know that it should take the value from a cell, the variable ‘a’ is placed in brackets after NETTOPREIS. If further cells are to be included in a function, a comma and the variable ‘b’ would follow, and so on.
So you type the following into the editor:
Function NET PRICE (a)
The editor should now look like this:
To ensure that Excel knows which calculation it is supposed to perform, you should 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 the following line must be entered in the editor:
NET PRICE=a/119100
If the function is to carry out further mathematical steps, these are simply written after the equals sign in accordance with the correct mathematical notation.
You can save the function by clicking on the floppy disc icon. If you have selected .xslx as the file format, you must save the file as .xslm, otherwise the file will not contain any macros.
Now you can check whether your function works the way you want it to.
About the authors
Wiresoft Editorial Team
The Wiresoft editorial team explains software, licences and how to use Windows, Office and servers in a clear and practical way. The articles are based on the questions that customers ask Wiresoft in their day-to-day lives.
Sources and further reading
Matching productsOffice