17

我有一个第 3 方 XLL 插件,我想将其包装在我自己的自定义 vba 函数中。如何从我的代码中调用 3rd 方函数?

4

2 回答 2

25

编辑:至少有两种方法可以做到这一点:


选项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

有关详细信息,请参阅此页面。

于 2008-12-17T19:36:30.623 回答
15

我知道这是一个迟到的答案,但我发现了这种替代方法并认为值得分享。您可以以与 Win32 调用相同的方式声明第 3 方函数。这具有额外的好处,即在您编码时显示在 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
于 2013-07-04T11:10:36.750 回答