繁体   English   中英

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

[英]How to pass as argument cells operation instead of single cell in 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:您需要在变量声明中指定类型,否则变量实际上是VariantDim det as Single, root1 as Single, root2 as Single... 虽然As Double这里As Double将是首选,以提高精度。

2:由于参数不再是Range ,因此请更改类型:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM