Hello Steve,
Here is a code routine to load the ranges you specify into a 2-D array. The code will pad the array with empty strings if the range length is less than the "rows" or second dimension of the array, and will not copy any "rows" beyond that limit.
The code prevemts any gaps by combining the individual ranges into a single contiguous range. This range is loaded into the 2-D array using loops, as Tom pointed out.
There are only 2 lines of code you need to modify to increase the number of ranges loaded. These lines are in bold type. One is the dimensions of Array_In and the other is the Variant Array called Ranges.
I realize this code is probably more complex than what you are used to. If you have any questions, you can email me LeithRoss@aol.com.
Sub FillArrayFromRanges()
Dim Array_In(1, 6)
Dim Element
Dim I As Long, J As Long, LastRow As Long
Dim Padding As Long
Dim RA As Range, Rng As Range
Dim Ranges, SubRng
Dim Temp()
Dim X As Long
Ranges = Array(Range("A2:A6"), Range("C1:C7"))
Set Rng = Ranges(0)
For Each SubRng In Ranges
Set Rng = Application.Union(Rng, SubRng)
Next SubRng
For I = 0 To Rng.Areas.Count - 1
Set RA = Ranges(I)
LastRow = RA.Rows.Count
If LastRow > UBound(Array_In, 2) + 1 Then LastRow = UBound(Array_In, 2) + 1
Padding = UBound(Array_In, 2) - LastRow + 1
For J = 1 To LastRow
ReDim Preserve Temp(X)
Temp(X) = RA.Cells(J, 1).Value
X = X + 1
Next J
If Padding > 0 Then
For J = 1 To Padding
ReDim Preserve Temp(X)
Temp(X) = ""
X = X + 1
Next J
End If
Next I
X = 0
For I = 0 To UBound(Array_In, 1)
For J = 0 To UBound(Array_In, 2)
Array_In(I, J) = Temp(X)
X = X + 1
Next J
Next I
End Sub
Sincerely,
Leith Ross
Bookmarks