使用Excel VBA中的工作表函数的值进行操作

我有两个函数,第一个函数,第二个函数的代码和第一个函数的代码几乎是一样的,但最后还是试图用最终结果进行一些算术运算。 第二个人不工作。 代码如下:

Public Function li(a As Double, b As Double) As Variant Dim mat(1 To 1, 1 To 3) As Variant Dim re As Variant Dim imat(1 To 3, 1 To 3) As Variant imat(1, 1) = 0.7409287 imat(1, 2) = -0.2849031 imat(1, 3) = 0.00002678114 imat(2, 1) = -0.2849031 imat(2, 2) = 0.1108151 imat(2, 3) = 0.00000774442 imat(3, 1) = -0.00002678114 imat(3, 2) = 0.00000774442 imat(3, 3) = 0.000000009373992 mat(1, 1) = 1 mat(1, 2) = a ^ 0.25 mat(1, 3) = b ^ 2 re = WorksheetFunction.MMult(WorksheetFunction.MMult(mat, imat), WorksheetFunction.Transpose(mat)) li = re End Function 'here the second function starts Public Function eli(a As Double, b As Double) As Variant Dim mat(1 To 1, 1 To 3) As Variant Dim re As Variant Dim imat(1 To 3, 1 To 3) As Variant imat(1, 1) = 0.7409287 imat(1, 2) = -0.2849031 imat(1, 3) = 0.00002678114 imat(2, 1) = -0.2849031 imat(2, 2) = 0.1108151 imat(2, 3) = 0.00000774442 imat(3, 1) = -0.00002678114 imat(3, 2) = 0.00000774442 imat(3, 3) = 0.000000009373992 mat(1, 1) = 1 mat(1, 2) = a ^ 0.25 mat(1, 3) = b ^ 2 re = WorksheetFunction.MMult(WorksheetFunction.MMult(mat, imat), WorksheetFunction.Transpose(mat)) eli = 1.96 * 126.948551122683 * (re + 1) ^ 0.5 End Function 

我猜在第二个问题是因为reworksheetfunction函数的值。 那么,我该如何解决这个问题呢?

debugging这个你会得到一个不匹配的错误在return语句。

eli = 1.96 * 126.948551122683 * (re + 1) ^ 0.5

看看本地窗口,你会看到re是一个variables数组,尺寸为1到1。

在这里输入图像说明

一个数组没有默认值属性,所以解决方法是引用数组索引:

eli = 1.96 * 126.948551122683 * (re(1) + 1) ^ 0.5

看到这个链接有关如何debugging你自己的代码的一些有用的提示。 干杯。