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

#### Swansonator

##### New Member
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)``

### Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

#### pgc01

##### MrExcel MVP
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
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
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

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
I'm glad it helped. You are right, the result is a Variant.

#### Swansonator

##### New Member

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
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.

#### Swansonator

##### New Member
Perfect! Thank you so much for your help and time.

#### pgc01

##### MrExcel MVP
You're welcome. Thanks for the feedback. Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

### Forum statistics

1,164,682
Messages
5,838,784
Members
430,568
Latest member
bortey ### 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.

### Which adblocker are you using?    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

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