Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call an xll addin function from vba?

I have a 3rd party XLL addin I'd like to wrap in my own custom vba function. How would I call the 3rd party function from my code?

like image 483
Dane O'Connor Avatar asked Dec 17 '08 18:12

Dane O'Connor


People also ask

How do you call an add-in function in VBA?

Make sure that you click on the workbook you want to add the reference to, and from the VBA editor menu choose Tools -> References. In the displayed list check the box beside your renamed add-in, and then click on OK. You'll see that your workbook now has a new reference to the add-in.

How do I use XLL add-ins?

XLL files can be opened with Microsoft Excel. If double-clicking an XLL file doesn't open it in Excel, you can do it manually via the File > Options menu. Select the Add-ins category and then choose Excel Add-ins in the Manage drop-down box. Choose the Go button and then Browse to locate it.

How do you call a function from a module in Excel VBA?

To call a macro or function that is in the same workbook (it need not be in the same module) just type the name of the macro/function and any arguments it requires on one line of the calling macro. Another way is to prefix the macro/function called with the word Call.


2 Answers

Edit: There are at least two ways to do this:


Option 1: Application.Run(...)

This looks like the best way to go about it, since your arguments are automatically converted to an appropriate type before being sent to the XLL function.

Public Function myVBAFunction(A as Integer, B as String, C as Double)
    myVBAFunction = Application.Run("XLLFunction", A, B, C)
End Sub

See this page for more details.


Option 2: Application.ExecuteExcel4Macro(...)

With this method, you will have to convert any arguments into string format before passing them to the XLL function.

Public Function myVBAFunction(A as Integer, B as String, C as Double)
    dim macroCall as String
    macroCall = "XLLFunction(" & A
    macroCall = macroCall & "," & Chr(34) & B & Chr(34)
    macroCall = macroCall & "," & C
    macroCall = macroCall & ")"
    myVBAFunction = Application.ExecuteExcel4Macro(macroCall)
End Sub

See this page for more details.

like image 99
e.James Avatar answered Oct 19 '22 12:10

e.James


I know this is a way late answer, but I discovered this alternate method and think it's worth sharing. You can declare the 3rd party functions in the same manner as a Win32 call. This has the added benefit of showing up in the Intellisense completion when you are coding.

Private Declare Function XLLFunction Lib "C:\PathTo3rdPartyDLL\3rdParty.xll" (ByVal A as Integer, ByVal B as String, C as Double) As Double

Sub Function myVBAFunction(A as Integer, B as String, C as Double) as Double
    myVBAFunction = XLLFunction(A, B, C)
End Sub
like image 32
CuberChase Avatar answered Oct 19 '22 12:10

CuberChase