Multidimensional Array Help

jaybee3

Active Member
Joined
Jun 28, 2010
Messages
307
Arrays always do my head in. Find a simple piece of code of what I'm trying to do.

As I would normally throw variables into an array using:

aTest = array(1,2,3,4,5)

Is it possible to do this for multiple dimensional ones?

Code:
Dim sTest(4, 4) As String
Dim ii As Integer, jj As Integer
sTest(0) = Array("hi", "Hello", "Bye", "No", "yes")
sTest(1) = Array("salut", "bonjour", "au revoir", "non", "Oui")
For ii = 0 To sTest.Rank - 1
    For jj = LBound(sTest(ii)) To UBound(sTest(ii))
        Debug.Print sTest(ii, jj)
    Next jj
Next ii
End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Not sure what you're ater, but is it this:
Code:
Dim sTest(4) 'As String
Dim ii As Integer, jj As Integer
sTest(0) = Array("hi", "Hello", "Bye", "No", "yes")
sTest(1) = Array("salut", "bonjour", "au revoir", "non", "Oui")
For ii = 0 To 1 'sTest.Rank - 1 '??
  For jj = LBound(sTest(ii)) To UBound(sTest(ii))
    Debug.Print sTest(ii)(jj)
  Next jj
Next ii
?
 
Upvote 0
You sir, are a champion :)

.rank, I thought returned the number of dimensions in a specified array?

This "sTest(ii)(jj)" format baffles me. As opposed to "sTest(ii,jj)" for example, which to me is what it would logically be?
 
Upvote 0
This "sTest(ii)(jj)" format baffles me. As opposed to "sTest(ii,jj)" for example, which to me is what it would logically be?

That's because in p45cal's example you are not using a bidimensional array, you are using a unidimensional array sTest(0 to 1) and then each of the elements of sTest() is itself an array. This is called usually a jagged array or an array of arrays.

That's why you use the "sTest(0)(1)", you are indexing the element of sTest with index 0, and then, since this element is itself an array you are indexing the element of this array with index 1.

In a bidimensional array you'd use something like "sTest(3,5)" where 3 and 5 are the indices of the first and second dimensions.

This is an example with a bidimensional array. Compare the 2. Both are interesting to know and may be handy in different situations.

Code:
Sub Test()
Dim sTest As Variant
Dim ii As Long, jj As Long
 
sTest = [{"hi", "Hello", "Bye", "No", "yes";"salut", "bonjour", "au revoir", "non", "Oui"}]
For ii = LBound(sTest, 1) To UBound(sTest, 1)
  For jj = LBound(sTest, 2) To UBound(sTest, 2)
    Debug.Print sTest(ii, jj)
  Next jj
Next ii
End Sub
 
Upvote 0
Wow that's infinitely clearer, thank you so much for the explanation!

And thank you for the bidimensional solution.

Just to continue on the discussion, can you combine the two then?

E.G. If each element in the bidimensional array was itself an array.

sTest = [{array("A","B","C"),array("D","E","F");array("G","H","I"),array("J","K","L")}]

sTest(0,0)(0). Would return "A" in this case?
 
Upvote 0
You got the idea. That's exactly that!

The syntax, however, is wrong.

This is an example.

Code:
Sub Test1()
Dim vArr(0 To 1, 0 To 1) As Variant
 
vArr(0, 0) = Array("A", "B", "C")
vArr(0, 1) = Array("D", "E", "F")
vArr(1, 0) = Array("G", "H", "I")
vArr(1, 1) = Array("J", "K", "L")
 
Debug.Print vArr(0, 0)(0) ' "A"
Debug.Print vArr(1, 1)(2) ' "L"
End Sub

... assuming the lower bound of the arrays is 0.
 
Last edited:
Upvote 0
one more thing you should be aware of when you start dealing with higher dimension arrays is that the nature of "redim preserve" changes.

it only lets you redimension the "highest" dimension.
 
Upvote 0
Can you only read from arrays using jagged arrays? Or can you plug values in to them too?

Code:
Dim sTest(4) 'As String
Dim ii As Integer, jj As Integer
sTest(0) = Array("hi", "Hello", "Bye", "No", "yes")

For ii = 0 To 4
  sTest(1)(ii) = ii
Next ii

For ii = 0 To 1   
  For jj = LBound(sTest(ii)) To UBound(sTest(ii))
    Debug.Print sTest(ii)(jj)
  Next jj
Next ii
 
Upvote 0

Forum statistics

Threads
1,216,115
Messages
6,128,919
Members
449,478
Latest member
Davenil

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
Back
Top