Redim Array of Arrays (Jagged Array)

AMAS

Active Member
Joined
Apr 11, 2010
Messages
472
Hi,

First attempt to creating an array of arrays and could you use guidance. I have declared the array:

Code:
Dim arrTemp     As Variant

Then I tried to redimension it (failed)

Code:
ReDim Preserve arrTemp (1)(1 to 500)
ReDim Preserve arrTemp (2)(1 to 500)
ReDim Preserve arrTemp (3)(1 to 500)

Finally will be filling the arrays:

Code:
arrTemp(1)(1) = "First array, first element"
arrTemp(2)(2) = "Second array, second element"
arrTemp(3)(3) = "Third array, third element"

How do I get this working?

AMAS
 
Thank you both. I will keep this thread handy because I am just starting to convert multiple arrays to a jagged array and expect to rely heavily on the posted examples.

Best wishes,

AMAS
 
Upvote 0

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi Rick, Amas

Remark:
I agree with Rick.


Jagged arrays are a very powerful and flexible tool, but more complex and less efficient than a simple multidimensional array. It's great to have them in your toolbox but they are to be used only when necessary. If a simple multidimensional array does the job that's the one you should use.


Amas, this is an example of what I'd answer to your post #1 in this thread if you did not specifically asked for a jagged array.


Notice that if you use a multidimensional array, you can not only (re)dimension it in one sweep at the beginning, but you can also easily redimension the last dimension using Preserve later, as you can see in the example.



Code:
Sub Test()
Dim arrTemp
 
 
ReDim arrTemp(1 To 3, 1 To 500)
 
 
arrTemp(1, 1) = "First array, first element"
arrTemp(2, 2) = "Second array, second element"
arrTemp(3, 3) = "Third array, third element"
 
 
' redim the second dimension of the array
ReDim Preserve arrTemp(1 To 3, 1 To 1000)
 
 
MsgBox _
    "arrTemp(1,1) = " & arrTemp(1, 1) & vbNewLine & _
    "arrTemp(2,2) = " & arrTemp(2, 2) & vbNewLine & _
    "arrTemp(3,3) = " & arrTemp(3, 3)
 
End Sub


Notice that, although not as flexible as a jagged array, this is a much simpler solution and, when adequate, that's the one to use.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,307
Members
452,633
Latest member
DougMo

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