Dealing with unknown array sizes
Hi again Robert,
I decided that it would be a worthwile exercise to create an example.
To test the example, enter some data in the first 5 columns of Sheet1 for 20
or so rows.
Run the code and it will populate the array and then output the contents of
the array to Sheet2.
Sub ReDimPreserveExample()
'Example of ReDim Preserve.
'Only last dimension can be ReDim Preserve
Dim myArr()
Dim C As Long 'Cols and 1st dimension in array
Dim R As Long 'Rows and 2nd dimension in array
With Sheets("Sheet1")
For C = 1 To 5
For R = 1 To 30
'Only ReDim on first loop of C
If C = 1 Then
ReDim Preserve myArr(1 To 5, 1 To R)
End If
myArr(C, R) = .Cells(R, C)
Next R
Next C
End With
MsgBox "# Elements in 1st dimension: " _
& UBound(myArr, 1) & vbLf & _
"# Elements in 2nd dimension: " _
& UBound(myArr, 2)
With Sheets("Sheet2")
For C = 1 To UBound(myArr, 1)
For R = 1 To UBound(myArr, 2)
.Cells(R, C) = myArr(C, R)
Next R
Next C
End With
End Sub
--
Regards,
OssieMac
|