如何从 vba 调用 xll 插件函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/375552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I call an xll addin function from vba?
提问by Dane O'Connor
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?
我有一个第 3 方 XLL 插件,我想包含在我自己的自定义 vba 函数中。我将如何从我的代码中调用 3rd 方函数?
Thanks
谢谢
回答by e.James
Edit:There are at least two ways to do this:
编辑:至少有两种方法可以做到这一点:
Option 1:Application.Run(...)
选项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.
这看起来是最好的方法,因为您的参数在发送到 XLL 函数之前会自动转换为适当的类型。
Public Function myVBAFunction(A as Integer, B as String, C as Double)
myVBAFunction = Application.Run("XLLFunction", A, B, C)
End Sub
See this pagefor more details.
有关更多详细信息,请参阅此页面。
Option 2:Application.ExecuteExcel4Macro(...)
选项 2:Application.ExecuteExcel4Macro(...)
With this method, you will have to convert any arguments into string format before passing them to the XLL function.
使用此方法,您必须先将任何参数转换为字符串格式,然后再将它们传递给 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
See this pagefor more details.
有关更多详细信息,请参阅此页面。
回答by CuberChase
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.
我知道这是一个迟到的答案,但我发现了这种替代方法,并认为值得分享。您可以以与 Win32 调用相同的方式声明第 3 方函数。这具有在您编码时显示在 Intellisense 完成中的额外好处。
Private Declare Function XLLFunction Lib "C:\PathTo3rdPartyDLLrdParty.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

