Populate a matrix, How?

  • Thread starter Thread starter Beto
  • Start date Start date
B

Beto

Hi,
I'm trying to populate a matrix but I can't figure it out a better way.

Example matrix:

[ 10 100 ]
[ 20 200 ]
[ 30 300 ]

I want a similar way to this: (that does not work)

Dim myMatrix As Variant
ReDim myMatrix(1 To 3, 1 To 2)
myMatrix = Array(Array(10, 20, 30), Array(100,200,300))

This redimensions the matrix to a 6 element array, so is not working as
I want it.
I know I can use "myMatrix(1,1) = 10" and so on, but I think there must
be a better way.
I also know that I can use a hidden sheet and then just copy the data
from the sheet to the matrix, but i'd like a only VBA code solution.

Can something like this be done?
Regards,
 
Dim myMatrix As Variant
myMatrix = Array(Array(10, 100), Array(20, 200), Array(30, 300))

Alan Beban
 
Alan,

How do your read the values from that? I get errors with anything
like:

Msgbox myMatrix(1,1)

Bernie
MS Excel MVP

Alan Beban said:
Dim myMatrix As Variant
myMatrix = Array(Array(10, 100), Array(20, 200), Array(30, 300))

Alan Beban
Hi,
I'm trying to populate a matrix but I can't figure it out a better way.

Example matrix:

[ 10 100 ]
[ 20 200 ]
[ 30 300 ]

I want a similar way to this: (that does not work)

Dim myMatrix As Variant
ReDim myMatrix(1 To 3, 1 To 2)
myMatrix = Array(Array(10, 20, 30), Array(100,200,300))

This redimensions the matrix to a 6 element array, so is not working as
I want it.
I know I can use "myMatrix(1,1) = 10" and so on, but I think there must
be a better way.
I also know that I can use a hidden sheet and then just copy the data
from the sheet to the matrix, but i'd like a only VBA code solution.

Can something like this be done?
Regards,
 
Bernie,

It's like the JavaScript multi-dimension arrays

MsgBox myMatrix(1)(1)

Regards

Bob

Bernie Deitrick said:
Alan,

How do your read the values from that? I get errors with anything
like:

Msgbox myMatrix(1,1)

Bernie
MS Excel MVP

Alan Beban said:
Dim myMatrix As Variant
myMatrix = Array(Array(10, 100), Array(20, 200), Array(30, 300))

Alan Beban
Hi,
I'm trying to populate a matrix but I can't figure it out a better way.

Example matrix:

[ 10 100 ]
[ 20 200 ]
[ 30 300 ]

I want a similar way to this: (that does not work)

Dim myMatrix As Variant
ReDim myMatrix(1 To 3, 1 To 2)
myMatrix = Array(Array(10, 20, 30), Array(100,200,300))

This redimensions the matrix to a 6 element array, so is not working as
I want it.
I know I can use "myMatrix(1,1) = 10" and so on, but I think there must
be a better way.
I also know that I can use a hidden sheet and then just copy the data
from the sheet to the matrix, but i'd like a only VBA code solution.

Can something like this be done?
Regards,
 
Bob,

Thanks!

Now, how do you read the dimensions? : )

TIA,
Bernie
MS Excel MVP who-learns-something-new-every-day
 
I figured it out. For example:

MsgBox UBound(myMatrix) - LBound(myMatrix) + 1 & " by " & _
UBound(myMatrix(0)) - LBound(myMatrix(0)) + 1 & " Matrix"

HTH,
Bernie
MS Excel MVP
 
myMatrix(0)(0) = 10
myMatrix(0)(1) = 100
myMatrix(1)(0) = 20
myMatrix(1)(1) = 200
myMatrix(2)(0) = 30
myMatrix(2)(1) = 300

Lbound(myMatrix) = 0
UBound(myMatrix) = 2

LBound(myMatrix(0))= 0
UBound(myMatrix(0))= 1
LBound(myMatrix(1))= 0
UBound(myMatrix(1))= 1
LBound(myMatrix(2))= 0
UBound(myMatrix(2))= 1

Alan Beban

Bernie said:
Alan,

How do your read the values from that? I get errors with anything
like:

Msgbox myMatrix(1,1)

Bernie
MS Excel MVP

Dim myMatrix As Variant
myMatrix = Array(Array(10, 100), Array(20, 200), Array(30, 300))

Alan Beban
Hi,
I'm trying to populate a matrix but I can't figure it out a better
way.
Example matrix:

[ 10 100 ]
[ 20 200 ]
[ 30 300 ]

I want a similar way to this: (that does not work)

Dim myMatrix As Variant
ReDim myMatrix(1 To 3, 1 To 2)
myMatrix = Array(Array(10, 20, 30), Array(100,200,300))

This redimensions the matrix to a 6 element array, so is not
working as
there must
data

solution.
 
Back
Top