neke
neke

Reputation: 85

Matrix Multiplication in VBA and storing values in another matrix

I stuck with pure matrix multiplication. I have two matrix: ArrW and ArrC. Each of them consists of 4 elements. The results should be saved in a third matrix, in the first slot: ArrWs(1). Afterwards I change the numbers in the ArrW and multiply this matrix with the same ArrC. The results should be stored in ArrWd(2).

During debugging the line ArrWs(x) = Application.WorksheetFunction.MMult(ArrC, ArrW) causes error. I do not know why. I checked the fulfilment of both matrix ArrW and ArrC. Each of them has 4 numbers- as planned.

Sub Matrix()

Dim TabelaEK As Range
Dim Size As Integer
Dim x As Integer
Dim y As Integer
Dim ArrW As Variant
Dim ArrWs As Variant
Dim ArrC As Variant

Set TabelaEK = ActiveSheet.ListObjects("ek").Range.Cells(1, 1)
Size = Range("E2").Value

ReDim ArrW(1 To Size)
ReDim ArrC(1 To Size)
ReDim ArrWs(1 To Size)

For x = 1 To Size 'Size is 4
    ArrW(x) = Cells(TabelaEK.Row + x, TabelaEK.Column + Size + 1)
Next
        'MsgBox ArrW(1)
        'MsgBox ArrW(2)
        'MsgBox ArrW(3)
        'MsgBox ArrW(4)

For x = 1 To Size
    For y = 1 To Size
        ArrC(y) = Cells(TabelaEK.Row + x, TabelaEK.Column - 1 + y)
    Next
        'MsgBox ArrC(1)
        'MsgBox ArrC(2)
        'MsgBox ArrC(3)
        'MsgBox ArrC(4)

    ArrWs(x) = Application.WorksheetFunction.MMult(ArrC, ArrW) 'here is the error!

Next
    'MsgBox ArrWs(1)
    'MsgBox ArrWs(2)
    'MsgBox ArrWs(3)
    'MsgBox ArrWs(4)
End Sub

Upvotes: 1

Views: 2198

Answers (1)

QHarr
QHarr

Reputation: 84465

MMULT requires the number of columns in array1 to match the number of rows in array2. So with one dimensional array you are saying 1 = UBound(ArrW) If there is more than one row then it should fail.

MMULT

The number of columns in Array1 must be the same as the number of rows in Array2, and both arrays must contain only numbers.

Upvotes: 1

Related Questions