HELP needed with VBA MMULT

mattchew

New Member
Joined
Apr 16, 2011
Messages
1
Hi guys,

I'm a total newbie and am trying to use mmult in VBA..
All i want to do is multiply 2 matrices (3x3 and 3x1), so that I can use that matrix for more complicated calculations.

Need help on a UDF that returns the final result as a matrix (i.e. 3x1 matrix)

Function matt()
Dim P(1 To 3, 3)
P(1, 1) = 1
P(1, 2) = 2
P(1, 3) = 3
P(2, 1) = 4
P(2, 2) = 5
P(2, 3) = 6
P(3, 1) = 7
P(3, 2) = 8
P(3, 3) = 9


Dim Q(1 To 3, 1)
Q(1, 1) = -1
Q(2, 1) = -2
Q(3, 1) = -3

Dim result(1 To 3, 1)
result = Application.WorksheetFunction.MMult(P, Q)
matt = result
End Function


Thanks in advance!!!

Matt
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Try this

Function matt() As Variant
Dim P(1 To 3, 1 To 3)
P(1, 1) = 1
P(1, 2) = 2
P(1, 3) = 3
P(2, 1) = 4
P(2, 2) = 5
P(2, 3) = 6
P(3, 1) = 7
P(3, 2) = 8
P(3, 3) = 9

Dim Q(1 To 3, 1 To 1)
Q(1, 1) = -1
Q(2, 1) = -2
Q(3, 1) = -3

matt = Application.WorksheetFunction.MMult(P, Q)
End Function
 
Upvote 0
Another way to initialize variables:
Code:
Function matt() As Variant
    Dim P           As Variant
    Dim Q           As Variant
 
    P = Evaluate("{1,2,3;4,5,6;7,8,9}")
    Q = Evaluate("{-1;-2;-3}")
    matt = WorksheetFunction.MMult(P, Q)
End Function
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,894
Members
452,948
Latest member
Dupuhini

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top