我正在编写一个自定义函数,目的是在Excel中对包含复数的多个数组执行乘法运算。例如,我在Excel工作表上定义了以下3个数组:
MATZ1:
0.99995021721684+0.0172827928509254i -0.040624595514759-0.000234038263707613i
0.00490168593281832-0.850839538828158i 0.99995021721684+0.0172827928509254i
MATZ2:
0.996262283629251+0.149720806155247i -0.952677113437938-0.0475875415517989i
0.0156809592632005-0.313924412139317i 0.996262283629251+0.149720806155247i
MATZ3:
0.998558978121825+0.0929765994909912i -0.128534373384092-0.00398493017536902i
0.0448308688243513-1.44602474297277i 0.998558978121825+0.0929765994909912i
我已经在VBA中编写了以下功能:
Function IMATMULT(rng1 As Range, rng2 As Range) As Variant
Dim i As Integer
Dim j As Integer
Dim l As Integer
Dim temp As String
Dim NumColumns As Variant
Dim NumRows As Variant
Dim NumRows2 As Variant
NumRows = rng1.Rows.Count - 1
NumColumns = rng2.Columns.Count - 1
If (rng1.Columns.Count = rng2.Rows.Count) Then
NumRows2 = rng1.Columns.Count
Else
IMATMULT = "non compatible arrays"
Exit Function
End If
Dim matrix() As String
ReDim matrix(NumRows, NumColumns)
For i = 0 To NumRows
For j = 0 To NumColumns
temp = "0"
For l = 1 To NumRows2
temp = WorksheetFunction.ImSum(temp, WorksheetFunction.ImProduct(rng1(i + 1, l).Value, rng2(l, j + 1).Value))
Next l
matrix(i, j) = temp
Next j
Next i
IMATMULT = matrix()
End Function
能够将两个复杂的数组相乘。当我在Excel中编写以下代码行时,会得到正确的结果:
但是,当我尝试将两个以上的数组相乘时,会出现错误结果:
我知道我可以一次只对一个数组进行一次乘法,但是我想知道为什么两次使用函数都行不通,因为我对VBA的当前了解不允许我理解原因为此。
非常感谢。
EDIT1:
似乎是问题在于此函数的输出格式与输入格式不同。要根据需要工作,此函数需要接受文本数组作为输入。但是,我不知道如何将这样的数组传递给函数,欢迎任何帮助。