VBA Matrix Operations - Why isn't this simple code working?

Swansonator

New Member
Joined
Apr 19, 2013
Messages
8
All I am trying to do is multiply two matrices together in vba. This code works if both 'a' and 'b' are square matrices, but this just returns an error, and 'c' has 'error 2015' when I'm trying to de-bug. Any help?

Code:
Sub arraytest()

Dim a(1 To 2, 1 To 2) As Variant
Dim b(1 To 2, 1) As Variant
Dim c As Variant


a(1, 1) = 1
a(1, 2) = 2
a(2, 1) = 3
a(2, 2) = 4


b(1, 1) = 5
b(1, 2) = 6


c = Application.MMult(a, b)


For i = 1 To 2
Cells(i, 1) = c(i, 1)
Next i


End Sub

On a related note, how can I assign hard coded values to a 2-d array? Such as

Code:
a = array(1, 2 ; 3,4)
 

Some videos you may like

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,884
Code:
Sub arraytest()

Dim b(1 To 2,[B][COLOR=#FF0000] 1[/COLOR][/B]) As Variant

...

b(1, 1) = 5
b(1, [B][COLOR=#FF0000]2[/COLOR][/B]) = 6

...

End Sub

Hi

You are referring to the column 2 in the matrix b, but this matrix does not have a column 2.

Can it be that you meant:

Code:
Dim b(1 To 2,[B][COLOR=#FF0000] 1 to 2[/COLOR][/B]) As Variant

?
 

Swansonator

New Member
Joined
Apr 19, 2013
Messages
8
I'm so sorry, that was from me changing things around to figure out the problem, I forgot to put them back. Even with that error fixed, it's still not working.

Code:
Sub arraytest()

Dim a(1 To 2, 1 To 2) As Variant
Dim b(1 To 2, 1) As Variant
Dim c As Variant


a(1, 1) = 1
a(1, 2) = 2
a(2, 1) = 3
a(2, 2) = 4


b(1, 1) = 5
b(2, 1) = 6


c = Application.MMult(a, b)


For i = 1 To 2
Cells(i, 1) = c(i, 1)
Next i


End Sub
 

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,884
Hi

I guess then that you want the second dimension to be 1?

Use:

Code:
Dim b(1 To 2, [B][COLOR=#FF0000]1 to 1[/COLOR][/B]) As Variant

Remarks:
1-
Code:
Dim b(1 To 2,[COLOR=#FF0000][B] 1[/B][/COLOR]) As Variant

is, by default

Code:
Dim b(1 To 2, [COLOR=#FF0000][B]0 to 1[/B][/COLOR]) As Variant

2 -
You forgot to declare the variable i

3 - Matrix operations use numbers. I usually declare the matirix elements as Double.
 

Swansonator

New Member
Joined
Apr 19, 2013
Messages
8

ADVERTISEMENT

Thank you! I had to keep c as a variant. It wouldn't work as a double, or if I gave it dimensions like this.

Code:
dim c(1 to 2, 1 to 1) as double

It worked just fine as follows:

Code:
Sub arraytest()

Dim a(1 To 2, 1 To 2) As Double
Dim b(1 To 2, 1 To 1) As Double
Dim c As Variant


a(1, 1) = 1
a(1, 2) = 2
a(2, 1) = 3
a(2, 2) = 4


b(1, 1) = 5
b(2, 1) = 6


c = Application.MMult(a, b)


For i = 1 To 2
Cells(i, 1) = c(i, 1)
Next i


End Sub
 

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,884
I'm glad it helped. You are right, the result is a Variant.
 

Swansonator

New Member
Joined
Apr 19, 2013
Messages
8

ADVERTISEMENT

Thanks, I was trying to solve this issue to work out a Newton-Raphson iteration macro of mine. I got everything working right, but when I multply two matrices in VBA the result is 0 ; 0, when it should be -0.1 ; -0.2. I msgbox the resulting matrix just to make sure Jinv and R are coming out like I think they would, but the multiplication of the two gives zeros.

Code:
Sub NewtonRaphson()
'f_1(x1,x2) = x_1^2 + x_2^2 - 4
'f_2(x1,x2) = x_1^2 + x_2 + 1
'J_1(x1,x2) = 2*x_1 + 2*x_2
'J_2(x1,x2) = 2*x_1 - 1


'definitions
Dim x(1 To 2, 1 To 1) As Double
Dim J(1 To 2, 1 To 2) As Double
Dim R(1 To 2, 1) As Double
Dim dx As Variant






'Initial Guess


x(1, 1) = 1
x(2, 1) = 2
R(1, 1) = 1
R(2, 1) = 1


Do Until Abs(R(1, 1)) < 0.0000000001 And Abs(R(2, 1)) < 0.0000000001


J(1, 1) = 2 * x(1, 1)
J(1, 2) = 2 * x(2, 1)
J(2, 1) = 2 * x(1, 1)
J(2, 2) = -1
R(1, 1) = -(x(1, 1) ^ 2 + x(2, 1) ^ 2 - 4)
R(2, 1) = -(x(1, 1) ^ 2 - x(2, 1) + 1)
Jinv = Application.MInverse(J)


strng = ""
For a = 1 To 2
strng = strng & Jinv(a, 1) & "  " & Jinv(a, 2) & "     " & R(a, 1) & vbNewLine
Next a
MsgBox (strng)


[COLOR=#ff0000]dx = Application.MMult(Jinv, R)[/COLOR]


strng = dx(1, 1) & vbNewLine & dx(2, 1)
MsgBox (strng)


For a = 1 To 2
x(a, 1) = x(a, 1) + dx(a, 1)
Next a


Loop


MsgBox (x(1, 1) & vbNewLine & x(2, 1))




End Sub
 

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,884
Hi

I couldn't test it yet, but I see again the same declaration error that you had before:

Code:
Dim R(1 To 2, [B][COLOR=#ff0000]1[/COLOR][/B]) As Double

It should be:

Code:
Dim R(1 To 2,[B] [COLOR=#ff0000]1 to 1[/COLOR][/B]) As Double

If that's not the whole problem it's at least a part of the problem.

Can you test it?

If it doesn't work I can test it later.
 

Watch MrExcel Video

Forum statistics

Threads
1,123,318
Messages
5,600,921
Members
414,416
Latest member
Nobu

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
Top