我试图编写一个简单的VBA函数,如下所示:
Public Function ReturnMMult(Arr1() As Variant, Arr2() As Variant) As Variant
ReturnMMult = WorksheetFunction.MMult(Arr1,Arr2)
End Function
但它总是给我#VALUE!我尝试将Arr更改为Ranges,但这也不起作用。我基本上希望能够编写可以采用$ A1:$ A10等类似范围的函数。看了很多地方,无法弄清楚。我做错了什么?
答案 0 :(得分:3)
类似
Public Function M(a As Excel.Range, b As Excel.Range) As Variant()
Dim a1() As Variant
Dim a2() As Variant
a1 = a.value
a2 = b.value
M = Application.WorksheetFunction.MMult(a1, a2)
End Function
答案 1 :(得分:1)
您需要将它们作为范围引入然后将它们转移到数组:
Public Function ReturnMMult(Arr1rng As Range, Arr2rng As Range) As Variant
Dim Arr1() As Variant: Arr1 = Arr1rng.Value
Dim Arr2() As Variant: Arr2 = Arr2rng.Value
ReturnMMult = WorksheetFunction.MMult(Arr1, Arr2)
End Function
答案 2 :(得分:0)
这是使用MMult工作表函数的一个示例。请根据您的情况采用它。
Sub test()
Dim xArray As Variant, yArray As Variant, zArray As Variant
Dim Fn As Object
Set Fn = Application.WorksheetFunction
xArray = Range("A1:B2").Value
yArray = Range("D1:E2").Value
zArray = Fn.MMult(xArray, yArray)
ActiveCell.Resize(2, 2).Value = zArray
End Sub
MMult返回#VALUE!错误时间: 任何单元格都是空的或包含文本。 array1中的列数与array2中的行数不同。 所得阵列的大小等于或大于总共5,461个单元。参考WorksheetFunction.MMult method