How to do Matrix Multiplication in excel VBA?
top of page

How to do Matrix Multiplication in excel VBA?


How to work with Matrices

vba matrix multiplication

Matrices could be a good alternative for when you start with bigger data and looping becomes slower. Normally should be fine with loops, but with that said. It could be useful to know that all things you do in excel can be done in vba as well, even processing matrices.

Sub Matrix()

'If incorrect Multiplication. #VALUE! will be on all cells that should be the result.

'Not really necessary to declare as they will be automatically variants.
'but could be good for practice purposes.
'Dim A as variant
'Dim B As Variant
'Dim C As Variant

A = Range("A2:B4") 'collecting matrix A
B = Range("D2:D3") 'collecting matrix B'Calculating Matrix C with .MMult syntax.
C = Application.MMult(A, B)

'Printing out me new matrix C
Range("F2:F4") = C

End Sub

Example 1)

multiply matrices

Example 2)

multiply arrays vba

Example 3) - if your matrices don't match.

matrix multiplication error

Learn more about VBA here for all my posts: https://www.pls-fix-thx.com/vba

If you have found this article or website helpful. Please show your support by visiting the shop below.




2,798 views0 comments
bottom of page