VBA矩阵乘法 [英] VBA Matrix multiplication MMult

查看:865
本文介绍了VBA矩阵乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正为运行时错误'1004':无法获取WorksheetFunction类的MMult属性"而苦苦挣扎.我使代码尽可能简单,但仍然无法正常工作.

I’m struggling with "Run-time error ‘1004’: Unable to get the MMult property of the WorksheetFunction class". I made the code as simple as possible but it still does not work.

我很感谢提示

Sub Matrix_Computation3()

Dim ws3 As Worksheet
Dim ArrP As Variant
Dim ArrCW As Variant
Dim Temp As Variant
Dim Size

 Set ws3 = Sheets("STEP 3")

 Size = 6

 ReDim ArrP(1 To 1, 1 To Size)
 ReDim ArrCW(1 To Size, 1)

 ArrP(1, 1) = 0.2
 ArrP(1, 2) = 0.2
 ArrP(1, 3) = 0.2
 ArrP(1, 4) = 0.2
 ArrP(1, 5) = 0.2
 ArrP(1, 6) = 0.2

 ArrCW(1, 1) = 0.3
 ArrCW(2, 1) = 0.3
 ArrCW(3, 1) = 0.3
 ArrCW(4, 1) = 0.3
 ArrCW(5, 1) = 0.3
 ArrCW(6, 1) = 0.3

 Temp = ws3.Application.WorksheetFunction.MMult(ArrP, ArrCW) ' here is an error
 MsgBox Temp(1)

End Sub

推荐答案

您正在传递MMULT错误的数组类型.这是一个工作表功能,而不是VBA功能.数组的工作表版本是一个单元格范围,因此将值放入两个工作表范围并在其中引用它们.

You're passing MMULT the wrong kind of array. This is a worksheet function, not a VBA function. The worksheet version of an array is a range of cells, so put the values into two worksheet ranges and refer to them there.

例如,您的部分代码可能是:

For example, part of your code might be:

Dim range1 as Range, range2 as Range
Set range1 = ws3.Range("A1:D1")
Set range2 = ws3.Range("E1:E4")
Temp = ws3.Application.WorksheetFunction.MMult(range1, range2)

还请注意, array1中的列数必须与array2中的行数相同,并且两个数组都只能包含数字.

Also note that the number of columns in array1 must be the same as the number of rows in array2, and both arrays must contain only numbers.

在尝试从VBA调用该函数之前,也许可以让该函数在工作表上正常工作.

Perhaps get the function working on the worksheet before trying to call it from VBA.

Office.com: WorksheetFunction.MMult方法 (VBA)

Office.com : WorksheetFunction.MMult method (VBA)

这篇关于VBA矩阵乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆