如何从vba调用xll addin函数?

时间:2008-12-17 18:31:48

标签: excel vba excel-vba ms-office

我有第三方XLL插件我想用自己的自定义vba功能包装。我如何从我的代码中调用第三方函数?

由于

2 个答案:

答案 0 :(得分:23)

修改:至少有两种方法可以做到这一点:


选项1: Application.Run(...)

这看起来是最好的方法,因为您的参数在被发送到XLL函数之前会自动转换为适当的类型。

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

有关详细信息,请参阅this page


选项2: Application.ExecuteExcel4Macro(...)

使用此方法,您必须在将任何参数传递给XLL函数之前将其转换为字符串格式。

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

有关详细信息,请参阅this page

答案 1 :(得分:13)

我知道这是一个迟到的答案,但我发现了这种替代方法,并认为值得分享。您可以使用与Win32调用相同的方式声明第三方功能。这有一个额外的好处,就是在编码时出现在Intellisense完成中。

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