功能集成besselk功能

时间:2017-02-17 14:16:24

标签: vba parameters double

我正在尝试创建一个将BESSELK函数作为参数的函数。我在fordom.free.fr上找到了这段代码:

Option Explicit

Function F(ByVal Equation As String, Optional ByVal X As Variant = 0) As Double
'Evaluates f(x) at position x

'Uppercase
Equation = UCase(Equation)
With WorksheetFunction
'avoid mixing up EXP with exp and X
Equation = .Substitute(Equation, "EXP", "exp")
'set value X instead of letter X
Equation = .Substitute(Equation, "X", X)
'converts comas into points
Equation = .Substitute(Equation, ",", ".")
'Changes )( with )*(
Equation = .Substitute(Equation, ")(", ")*(")
End With

'calculates expression
F = Evaluate(Equation)

End Function

并修改它以考虑2个参数:

Option Explicit

Function F(ByVal Equation As String, Optional ByVal X As Variant = 0, _
& **Optional ByVal Y As Variant = 0**) As Double
'Evaluates f(x) at position x

'Uppercase
Equation = UCase(Equation)
With WorksheetFunction
'avoid mixing up EXP with exp and X
Equation = .Substitute(Equation, "EXP", "exp")
'set value X instead of letter X
Equation = .Substitute(Equation, "X", X)
**'set value Y instead of letter Y
Equation = .Substitute(Equation, "Y", Y)**
'converts comas into points
Equation = .Substitute(Equation, ",", ".")
'Changes )( with )*(
Equation = .Substitute(Equation, ")(", ")*(")
End With

'calculates expression
F = Evaluate(Equation)

End Function

当我尝试F(“x + y”; 5; 2)时,它返回7,这是正常的。但是,当我尝试 F(“BESSELK(x; y)”; 5; 1),它给出了一个#VALUE错误

我检查了文档和变量类型或贝塞尔函数中的两个参数确实是“Variante”。

我真的不明白为什么会出错!我也尝试了一个更简单的函数F(“POWER(x; y)”; 5; 2)但它也给了我一个#VALUE错误。

欢迎任何帮助!非常感谢你。

1 个答案:

答案 0 :(得分:0)

这些行的想法

  

Equation = .Substitute(Equation, "EXP", "exp")

     

Equation = .Substitute(Equation, "Y", Y)

是形式参数XY应该在公式内的大写中编写。

=F("BESSELK(X;Y)";5;1) ' <-- Capital X, capital Y
=F("POWER(x;y)";5;2)   ' <-- idem

请注意,此UDF仅适用于您的区域设置。