Sure to enjoy this one. Lets you create Arrays within Arrays. Believe it or not, a very handy piece of code. Can be a memory hog though so use judiciously. To understand it fully, open your immediate window, and locals window then put a break in the code to step through it, and watch the variables.
Code
Option Base 1
Private Type T_small
MArray2() As String
End Type
Sub Array_In_Array()
'USE - VBA ARRAY WITHIN ARRAY
'BROUGHT TO YOU BY YOUR www.programminglibrary.com
'OPEN SOURCE RULES!!!!
'DEC VARS
Dim MArray(10) As T_small ' SET SIZE OF MAIN ARRAY
'*******************************************************************
'FIRST BLOCK USED TO CREATE SIZE OF INNER ARRAY (MARRAY2)
'LOOP TO CREATE SIZE OF NEW VIRTUAL INNER ARRAY -OPTION BASE 1 MAKES IT START AT ONE INSTEAD OF 0
'IN THIS EXAMPLE WE WILL MAKE THE INNER ARRAY A SET VALUE OF 5, COULD BE ANYTHING OR DYNAMIC
'*******************************************************************
For x = 1 To 10
ReDim Preserve MArray(x).MArray2(5) 'RESIZE INNER ARRAY
Next x
'*******************************************************************
'SECOND BLOCK PUTTING VALUES INTO INNER ARRAY
'NOTE - YOU NEED 2 LOOPS TO CYCLE THRU, 1 FOR OUTTER ARRAY, ONE FOR THE INNER
'*******************************************************************
For x = 1 To 10
For xx = 1 To 5
MArray(x).MArray2(xx) = xx 'STORE VALUE TO INNER ARRAY - JUST STORING NUMBER HERE
Next xx
Next x
'*******************************************************************
'THIRD BLOCK IS TO READ CONTENTS, PRINTS TO THE IMMEDIATE WINDOW IF YOUR
'ARE IN CODE STEP MODE - SET BREAKPOINT IN CODE. CNTL + G FOR IMMEDIATE WINDEOW
'*******************************************************************
' DEBUG PRINT RESULTS
For x = 1 To 10
Debug.Print "----- MArray: " & x & " -----"
Debug.Print "----- Elements: " & UBound(MArray(x).MArray2) & " -----"
For xx = 1 To UBound(MArray(x).MArray2)
Debug.Print xx & ": " & MArray(x).MArray2(xx)
Next xx
Next x
End Sub
Display More