excel - 如何在 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:需要在变量声明中指定类型,否则变量实际上是Variant
s: Dim det as Single, root1 as Single, root2 as Single...
。虽然As Double
这里会更精确。
2:由于参数不再Range
是s,改变类型:
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