View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Joe Dunfee Joe Dunfee is offline
external usenet poster
 
Posts: 10
Default Why use redim when creating an array of arrays?

As I got more into using the code, I ran into problems. The master-array
elements must set equal to the sub-arrays only AFTER the values are entered
for the sub-arrays. It seems that once you create the array-within-an-array,
the values of the elements are set and cannot be changed. Here is my sample
code with comments;

'==================
Sub ABC()
Dim vArr1(1 To 10) As Variant
Dim vArr2(1 To 5) As Variant
Dim vMainArr(1 To 2) As Variant
Dim lCounter As Long
Dim s As String, i As Long, j As Long

' Populate the sub arrays:
For lCounter = 1 To 10
vArr1(lCounter) = lCounter
If lCounter < 6 Then _
vArr2(lCounter) = lCounter * 2
Next lCounter

' Assign the sub arrays to the main array:
' Note that this must be done after the sub arrays are populated with values
vMainArr(1) = vArr1
vMainArr(2) = vArr2

'change one of the elements as a test
vArr2(2) = 100 ' note that this change WON'T show in the vMainArr array

' Show the results
s = ""
For i = 1 To 2
For j = LBound(vMainArr(i), 1) To UBound(vMainArr(i), 1)
s = s & vMainArr(i)(j) & ","
Next j
s = s & vbNewLine
Next i
MsgBox s

End Sub

'==================

Joe Dunfee