PDA

View Full Version : Calculate the product of two matrices



njrobby
10-04-2015, 08:20 PM
Hello,

Yes, I'm a student with an assignment.
But, I fell like I'm most of the way to my solution, although my code isn't working.
I have written the code below to calculate the product of two matrices but I keep getting #NAME? instead of a matrix.

14503

I'm running in Excel - Microsoft Visual Basic 14.5.5 for Mac

Any help would be much appreciated!
Thanks



Public Function MultMatrix(A As Range, b As Range) As Variant


'Define Variables
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim C() As Variant
Dim Ctemp As Variant
'Dim A() As Variant

'Set initial Values
i = 1
j = 1
k = 1
C(i, j) = 0

'Matrix Multiplication

For i = 1 To 3
For j = 1 To 3
For k = 1 To 3


Ctemp = A(i, j).Value * b(j, i).Value
C(i, j).Value = C(i, j).Value + Ctemp
A(i, j) = C(i, j) + Ctemp(i, j) + A(i, j)
Next k
Exit For
Next j
Exit For
Next i
'Exit For
End Function

mancubus
10-04-2015, 11:48 PM
welcome to the forum.

homework: work on below subjects...

1 creating user defined functions...
for ex: http://www.cpearson.com/excel/writingfunctionsinvba.aspx

2 loops in general... specifically exiting a loop...
for ex: http://www.excelfunctions.net/VBA-Loops.html

3 arrays
for ex: http://www.snb-vba.eu/VBA_Arrays_en.html

snb
10-05-2015, 12:12 AM
Too simple:

=PRODUCT(B22:B25;F22:H25)

or

=MMULT(B22:B25;F22:H25)