Writing VBA code is not for everyone. But nonetheless, here is a short-and-sweet example. If you can conquer this, you may want to find out more about programming VBA. Who knows — maybe one day you'll be churning out sophisticated functions of your own! Make sure you are working in a macro-enabled workbook (one of the Excel file types).
Follow along to create custom functions:
Press Alt + F11.
This gets you to the Visual Basic Editor, where VBA is written.
You can also click the Visual Basic button on the Developer tab of the Ribbon. The Developer tab is visible only if the Developer checkbox is checked on the Customize Ribbon tab of the Excel Options dialog box.
Choose Insert→Module in the editor.
You have an empty code module sitting in front of you. Now it's time to create your very own function!
Type this programming code, shown in the following figure:Writing your own function
Public Function Add(number1 As Double, number2 As Double) Add = number1 + number2 End Function
Save the function.
Macros and VBA programming can be saved only in a macro-enabled workbook.
After you type the first line and press Enter, the last one appears automatically. This example function adds two numbers, and the word Public lists the function in the Insert Function dialog box. You may have to find the Excel workbook on the Windows taskbar because the Visual Basic Editor runs as a separate program. Or press Alt+ F11 to toggle back to the Workbook.
Return to Excel.
Click the Insert Function button on the Formulas tab to display the Insert Function dialog box.Finding the function in the User Defined category
The Function Arguments dialog box opens, ready to receive the arguments. Isn't this incredible? It's as though you are creating an extension to Excel, and in essence, you are.Using the custom Add function
Macro-enabled workbooks have the file extension .xlsm.