Binding for functions in an Automation Add-in is at the end of the function binding precedence. If you have a function in your Automation Add-in with the same name as an Excel built-in function, the Excel built-in function takes precedence. Visual Basic for Automation (VBA) functions in workbooks and regular add-ins (.xla) also take precedence over Automation Add-in functions. When you create your own functions for use in Excel, Microsoft recommends that you do not name your functions with a name that is already in use by an Excel built-in function.
Steps to Reproduce Behavior
Create the Automation Add-Ins:
- Start a new AddIn project in Visual Basic. By default, a form named frmAddIn is added to the project.
- On the Project Explorer, right-click the form, and select Remove frmAddIn from the context menu to remove the form.
- On the Project menu, select MyAddin Properties. Change the Project Name to aTest1, and then click OK.
- On the Project Explorer, select the Connect designer. Change its Name property to XLFunctions.
- Double-click the XLFunctions designer in the Project Explorer. Make the following changes to the designer settings on the General tab:
- From the Application drop-down list, select Microsoft Excel.
- From the Application Version drop-down list, select Microsoft Excel 2002.
- Change the Initial Load Behavior to Load on demand. - With the XLFunctions designer still open, select Code on the View menu. Delete all the existing code in the module. Replace the code in the module with the following:
Option Explicit
Public Function SUM(Num1 As Variant, Num2 As Variant) As String
On Error Resume Next
Dim retVal As Double
retVal = CDbl(Num1) + CDbl(Num2)
SUM = "My Sum: " & retVal
End Function
Public Function DLLName() As String
DLLName = App.EXEName
End Function
- Save the project as aTest1.vbp.
- Build the add-in as aTest1.dll.
- Repeat steps 1 through 8 except now use aTest2 as the project name.
Now there are two separate projects, aTest1 and aTest2.
Test the Automation Add-ins in Excel:
- Start Microsoft Excel.
- On the Tools menu, click Add-Ins to display the Add-Ins dialog box.
- Click Automation, select aTest1.XLFunctions in the list, and then click
OK. Check aTest1.XLFunctions, and then click OK to close the Add-in dialog box.
- Go to cell A1 and, on the Insert menu, click Function. In the category drop-down list, select aTest1.XLFunctions. Then select the SUM function from the list box and click OK. Enter 10 for Num1 and enter 20 for Num2, and then click OK.RESULT: The Function Wizard used the built-in SUM function instead.
- In cell A2, type the following formula:
=aTest1.XLFunctions.SUM(10,20)
RESULT: "My Sum: 30" is displayed in the cell. This is the SUM function from your Automation Add-in. However, note that the formula reads:
=SUM(10,20) for cell A2.
- Edit the formula in A2 to sum the numbers 10, 40.
RESULT: Cell A2 is now bound to the built-in SUM function as a result of re-entering the formula. In order to use the custom SUM function, you need to include the ProgID of the add-in when you enter the formula: =aTest1.XLFunctions.SUM(10,40). - On the Tools menu, click Add-Ins to display the Add-Ins dialog box.
- Click Automation, select aTest2.XLFunctions in the list, and then click
OK. Check aTest2.XLFunctions, and then click OK to close the Add-ins dialog box.
- Go to cell B1 and, on the Insert menu, click Function. In the category drop-down list, select aTest1.XLFunctions, select the DLLName function from the list box, and then click OK twice.
- Go to cell B2 and, on the Insert menu, click Function. In the category drop-down list, select aTest2.XLFunctions, select the DLLName function from the list box, and then click OK twice.
RESULT: Cell B2 is using the DLLName function from aTest1.dll, resulting in both cells B1 and B2 displaying aTest1.