Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Create Custom Excel Functions

First make sure that we are working in a macro-enabled workbook (one of the Excel file types)
because we need to work with VBA code.
To create the custom (User defined) function in Excel:
1. Go to Microsoft Visual Basic Editor for Applications window (Press Alt + F11)
This gets us to the Visual Basic Editor, where VBA is written. OR
Click the Developer tab of the Ribbon > Visual Basic button.
If the Developer tab is not visible then check the Developer checkbox on the Customize Ribbon
tab of the Excel Options dialog box.
Excel Option > Customize Ribbon > Check Developer.
2. Choose InsertModule in the editor.
This bring us an empty code module in front. Now its time to create the custom excel function.
The example code is:
Public Function Add(number1 As Range, number2 As Double)
Add = number1 + number2
End Function
This function takes two arguments number1 and number2. The first argument, number1, is
value taken from another excel cell and the second argument, number2, is number.
And value must be assigned to the variable with same name as that of the function, which is
returned to the cell in which this function is used.
3. Save the function.
Macros and VBA programming can be saved only in a macro-enabled workbook.
4. Use the custom function
Now return to excel sheet and use the custom user defined function as standard excel function.

Use a Custom Function in another Workbook


By default, a user-defined function is limited in scope to the workbook in which the function
resides. In other words, only sheets within the same workbook can call a user-defined function.
If we need to call a user- defined function from another workbook, we must first make a
reference to the workbook that contains the user-defined function.
To add reference of the workbook (Say workbook B) that contains the user-defined function in
another workbook in which we want to use the custom function (say workbook A):
1. In workbook A, open the Microsoft Visual Basic for Applications window.
2. Select Tools > References.
3. In the References dialog, choose Browse.
4. In the Add Reference dialog that appears,
Choose Microsoft Excel Files from the Files of type box,
Select the file that you want to call (B), and choose Open.
5. Choose OK to close the References dialog.
If Name conflicts with existing module, project or object library error appears then change
VBA project name as below:

Go to Microsoft Visual Basic for Applications window


Then Tools > VBAProject properties
In VBAProject properties window go to General tab > change the Project Name field.
Now in file A, we should be able to call public module-level functions in file B as if they were
in file A. Even this function can be used in file A, it may not be shown by excel as assistance
for autocomplete on typing.

Create a Global User-Defined Function


To be able to call a user-defined function from any file, new or existing, we have to manually
create a reference in each and every workbook. To make your user-defined functions globally
available, we can group them into one workbook and make an Add-in.
To make Add-in save excel workbook as Excel Add-in (*.xlam).
Once saved and re-opened the Workbook, saved in .xla format, will be hidden and can only be
seen in the "Project Explorer" via the Visual Basic Editor.
Once the Add-in is created, it cannot be edited.
You may specify a name for your Add-in. To name an Add-In
in Microsoft Excel version 5.0, follow these steps:
o On the File menu, click Summary Info.
o Type the name in the Title box, and click OK.
in Microsoft Excel version 7.0, follow these steps:
o On the File menu, click Properties.
o Type the name in the Title box, and then click OK.
The Add-in must be loaded into memory for our custom functions to be available. To load
Add-in in memory:
Placed the Add-in in XLSTART folder (directory)
o For Windows: usually "C:\MSOFFICE\EXCEL\XLSTART" (without the
quotation marks) and
o For Macintosh: in the Excel Startup Folder on the Macintosh, either
"System:Preferences:Excel Startup Folder (5)" or "System:Preferences:Excel
Startup Folder" (without the quotation marks).
Or load the Add-in using the Add-in Manager using following steps:
o Go to Add-ins Developer tab > click Add-ins
o On the Add-ins window, click Browse.
o Locate the file, click to select it, and then click OK.
o Ensure the Add-In is In The Add-Ins Available: Box And It Is Checked.

You might also like