Advice Required: 2D Array within an Array

fat-tony

New Member
Joined
May 12, 2011
Messages
29
Hi guys,

I am playing with putting an array within an array e.g. TLArray(i).SubArray(3,100)

Code:
Option Base 1

Private Type arrayType
    SubArray() As String
End Type

Sub Array_Array()
    Dim TLArray(20) As arrayType
    For i = 1 To 20
        ReDim Preserve TLArray(i).SubArray(3, 100)
    Next i
    For i = 1 To 20
       For x = 1 To 3
          For y = 1 To 100
              TLArray(i).SubArray(x, y) = "Stuff Goes Here"
          Next y
       Next x
    Next i
End Sub
the TLArray will have 20 Sub Arrays. The SubArrays will have 300 entries each in total (3 sets of 100).

Just how much of a memory hog do you guys think this would be?

I was originally thinking of doing: MyArray(20,3,100) would that be a better solution?

Code:
Sub Multi_Array()
    Dim SubArray() As String
    ReDim Preserve SubArray(20, 3, 100)
    For i = 1 To 20
       For x = 1 To 3
          For y = 1 To 100
              SubArray(i, x, y) = "Stuff Goes Here"
          Next y
       Next x
    Next i
End Sub
Thanks guys.

All thoughs greatly appreciated.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
For those sizes I don't think it makes any difference - for (1) though you need it like this:

Code:
Dim myArr(1 to 20)
Dim mySubArr As Variant

mySubArr = Range("A1:C100").Value

MyArr(1) = mySubArr

'etc etc

'then access using:

myArr(1)(98,3) = "A New Value"
 
Upvote 0
For those sizes I don't think it makes any difference - for (1) though you need it like this:

Code:
Dim myArr(1 to 20)
Dim mySubArr As Variant

mySubArr = Range("A1:C100").Value

MyArr(1) = mySubArr

'etc etc

'then access using:

myArr(1)(98,3) = "A New Value"

re: memory usage thats what I thought. Thanks.

I can't use range as I am not going to populate the arrays from ordered cell values like that.

But I do keep forgetting to use .range when it should be used. So thanks, for the reminder.
 
Upvote 0
The Range wasn't important - just used it to populate a 2D array quickly. The essentials of that method is that the parent array holds a further array (2D in this case) in each element (and to access the 'sub array' you need to use the code form given).
 
Upvote 0
The Range wasn't important - just used it to populate a 2D array quickly. The essentials of that method is that the parent array holds a further array (2D in this case) in each element (and to access the 'sub array' you need to use the code form given).

Sorry I am a bit confused, can you talk me through the differences as to what I used in the first example. What am I missing? Thanks.
 
Upvote 0
I don't see any memory issues, but Excel's implementation of UD Types is quirky. I would probably go with a native VBA 3D array.
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,763
Members
452,940
Latest member
rootytrip

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