How to create Custom Excel Functions using VBA
Microsoft Excel comes with many pre-defined functions which do the maximum task for the states. We never demand any more functions other than those congenital-in functions in most of the cases. But, what if yous are in need of some functionality which was not being provided by whatsoever pre-divers Excel function?
Microsoft Excel allows u.s.a. to create Custom Excel Functions or User Divers Functions using VBA. We can create Custom Excel Functions with the functionality we want and they can exist accessed in the Excel Sheet every bit regular Excel Functions using "=" followed by the function proper noun. I volition take you through the steps of creating custom Excel Functions using VBA.
Create Custom Excel Functions
Since we will exist creating the Custom Excel Function using VBA, nosotros need to enable the "Developer" tab first. By default, information technology is not enabled and we can enable it. Open the Excel Sail and click on the Excel push button and then click on "Excel Options". And then check the box, beside "Prove Programmer tab in the Ribbon".
Now, to open up the Visual Bones Editor, tap on the Developer tab and click on "Visual Basic" icon to launch the Visual Basic Editor.
You tin even use the keyboard shortcut "Alt + F11" to launch the Visual Basic Editor. If you lot use this keyboard shortcut, then at that place is no need to enabling the Programmer tab also.
Now, everything is set to create the Custom Excel Function. Correct click on the "Microsoft Excel Objects", click on "Insert" and so click on "Module".
It opens the plain window which is the identify to write code.
Before, writing the code, y'all demand to understand the sample syntax which needs to be followed to create Custom Excel Office and here how it is,
Function myFunction (arguments) return type
myFunction = some_calculation
End Function
There is no 'Return' argument as we accept with normal programming languages.
Insert your lawmaking in the plain window which just opened. For example, I will create a function "FeesCalculate" which calculates the '8%' of the value provided to the function. I used the return type as "Double" every bit the value might be in decimals likewise. You tin can come across that, my code follows the syntax of VBA.
Now, it is the time to save the Excel workbook. Save it with the extension of '.xslm' to utilise excel sheet with Macro. If y'all do non salvage information technology with this extension, it throws an error.
That's information technology!
Now, you lot can employ the User Defined Function in the Excel sail as a normal Excel function using "=". When you start typing "=" in the prison cell, it shows you the created function along with other built-in function.
Y'all can run across the example below:
Excel Custom Functions cannot change the environment of Microsoft Excel and thus they accept limitations.
Limitations of Custom Excel Functions
Custom Excel Functions cannot do the following,
- Insert, format, or delete cells on the spreadsheet.
- Changing the value of another cell.
- Calculation names to the workbook.
- Rename, delete, move or add sheets to the workbook.
There are many more such limitations and mentioned are some of them.
These are the simple steps to be followed to create Custom Excel Functions.

Source: https://www.thewindowsclub.com/create-custom-excel-functions
Posted by: fowlerfortall.blogspot.com
0 Response to "How to create Custom Excel Functions using VBA"
Post a Comment