如何在 Excel VBA UDF 中传递为参数单元格操作而不是单个单元格?



>我创建了一个UDF来计算一个非线性方程,该方程考虑3个参数(A1,B1和C1(。结果以 D1 (=SLV(A1;B1;C1((。

我想做的是在单元格之间给出一个操作,而不是 A1、B1 或 C1。 例如:我想写 G1/H1^(K1*10^-4(,而不是 A1。

我的 UDF 函数如下:

Function SLV(ByVal a, b, c As Range) As Single
Dim det, root1, root2, rootf As Single
<several operations>
SLV = result
End Function

这会导致错误#VALUE

提前非常感谢。

1:需要在变量声明中指定类型,否则变量实际上Variants:Dim det as Single, root1 as Single, root2 as Single...。尽管为了更精确,这里最好使用As Double

2:由于参数不再Ranges,因此更改类型:

Function SLV(ByVal a As Double, ByVal b As Double, ByVal c As Double) As Double
Dim det As Double, root1 As Double, root2 As Double, rootf As Double, result As Double
<several operations>
SLV = result
End Function

最新更新