我有一个第三方XLL插件,我想用自己的自定义vba函数包装.我如何从我的代码中调用第三方功能?
谢谢
编辑:至少有两种方法可以做到这一点:
选项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
有关详细信息,请参阅此页面.
选项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
有关详细信息,请参阅此页面.
我知道这是一个迟到的答案,但我发现了这种替代方法,并认为值得分享.您可以使用与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