How to merge multiple ranges into one array.

JOAO12

New Member
Joined
Feb 1, 2013
Messages
12
Office Version
  1. 2013
Platform
  1. Windows
Hy everyone,

The below code works when the ranges are A5:J39, A44:J65 and A70:J89

VBA Code:
Sub RangesToArray()
  Dim vRws As Variant, vData As Variant
  
  vRws = Application.Transpose(Split( _
          Join(Application.Transpose(Evaluate("row(5:39)"))) & " " & _
          Join(Application.Transpose(Evaluate("row(44:65)"))) & " " & _
          Join(Application.Transpose(Evaluate("row(70:89)")))))
  vData = Application.Index(Cells, vRws, Application.Transpose(Evaluate("row(1:10)")))
End Sub

What should be changed so that it works with the ranges B3:F13, I3:M20 and P30:T49 ? The final array (vData) should have 49 rows (11 + 18 + 20) by 5 columns.

Thanks.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
This approach doesn't generalise well, without a lot of contrivance.

You're probably better off just combining the arrays in VBA. Perhaps something like:

VBA Code:
Sub Test()

    Dim v As Variant
    
    v = RangesToArray(Range("B3:F13"), Range("I3:M20"), Range("P30:T49"))
    Range("V1").Resize(UBound(v), UBound(v, 2)).Value = v
        
    'Or with Excel 365, a simple:
    Range("V1").Formula2 = "= RangesToArray(B3:F13,I3:M20,P30:T49)"
        
End Sub
Function RangesToArray(ParamArray rng() As Variant) As Variant

    Dim i As Long, r As Long, c As Long, NoRows As Long, NoCols As Long, Count As Long
    Dim InArr() As Variant, OutArr() As Variant
    ReDim InArr(LBound(rng) To UBound(rng))

    For i = LBound(rng) To UBound(rng)
        InArr(i) = rng(i).Value
        NoRows = NoRows + UBound(InArr(i))
        NoCols = Application.Max(NoCols, UBound(InArr(i), 2))
    Next i
    ReDim OutArr(1 To NoRows, 1 To NoCols)

    For i = LBound(InArr) To UBound(InArr)
        For r = 1 To UBound(InArr(i))
            For c = 1 To UBound(InArr(i), 2)
                OutArr(r + Count, c) = InArr(i)(r, c)
            Next c
        Next r
        Count = Count + r - 1
    Next i

    RangesToArray = OutArr

End Function
 
Upvote 0
Solution
Stephen,
Thank you very much. Your code solved this problem.
 
Upvote 0
What Excel version are you using?

I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

Also wondering what you are going to do with the new single array? Is it going back onto the worksheet?
 
Last edited:
Upvote 0
The reason that I was asking about version is that IF you have Microsoft 365 and happen to have the latest functions that are gradually being rolled out to subscribers, you can do this very easily.

Here is a smaller example where my original ranges are the green ones and the code below has produced the blue one.

And if you happen to be doing this in vba because it is difficult with formulas, with the new VSTACK function it just requires a single formula in a single cell (yellow) to produce the same results without vba.

22 06 09.xlsm
BCDEFGHIJKLMNOPQRST
1
2
3A 1 - 65A 1 - 5A 1 - 41A 1 - 60A 1 - 92A 2 - 7A 2 - 49A 2 - 81A 2 - 62A 2 - 93
4A 1 - 85A 1 - 5A 1 - 34A 1 - 53A 1 - 78A 2 - 81A 2 - 68A 2 - 96A 2 - 7A 2 - 19
5A 1 - 78A 1 - 65A 1 - 7A 1 - 42A 1 - 22
6A 1 - 84A 1 - 57A 1 - 37A 1 - 62A 1 - 99A 3 - 7A 3 - 6A 3 - 16A 3 - 56A 3 - 82
7A 3 - 87A 3 - 20A 3 - 56A 3 - 5A 3 - 51
8A 3 - 20A 3 - 95A 3 - 32A 3 - 84A 3 - 16
9A 3 - 81A 3 - 20A 3 - 9A 3 - 95A 3 - 37
10A 1 - 65A 1 - 5A 1 - 41A 1 - 60A 1 - 92A 1 - 65A 1 - 5A 1 - 41A 1 - 60A 1 - 92A 3 - 12A 3 - 9A 3 - 80A 3 - 76A 3 - 52
11A 1 - 85A 1 - 5A 1 - 34A 1 - 53A 1 - 78A 1 - 85A 1 - 5A 1 - 34A 1 - 53A 1 - 78A 3 - 6A 3 - 70A 3 - 56A 3 - 31A 3 - 74
12A 1 - 78A 1 - 65A 1 - 7A 1 - 42A 1 - 22A 1 - 78A 1 - 65A 1 - 7A 1 - 42A 1 - 22A 3 - 43A 3 - 35A 3 - 21A 3 - 67A 3 - 21
13A 1 - 84A 1 - 57A 1 - 37A 1 - 62A 1 - 99A 1 - 84A 1 - 57A 1 - 37A 1 - 62A 1 - 99A 3 - 78A 3 - 66A 3 - 34A 3 - 61A 3 - 60
14A 2 - 7A 2 - 49A 2 - 81A 2 - 62A 2 - 93A 2 - 7A 2 - 49A 2 - 81A 2 - 62A 2 - 93A 3 - 81A 3 - 96A 3 - 42A 3 - 42A 3 - 1
15A 2 - 81A 2 - 68A 2 - 96A 2 - 7A 2 - 19A 2 - 81A 2 - 68A 2 - 96A 2 - 7A 2 - 19A 3 - 5A 3 - 61A 3 - 13A 3 - 84A 3 - 18
16A 3 - 7A 3 - 6A 3 - 16A 3 - 56A 3 - 82A 3 - 7A 3 - 6A 3 - 16A 3 - 56A 3 - 82
17A 3 - 87A 3 - 20A 3 - 56A 3 - 5A 3 - 51A 3 - 87A 3 - 20A 3 - 56A 3 - 5A 3 - 51
18A 3 - 20A 3 - 95A 3 - 32A 3 - 84A 3 - 16A 3 - 20A 3 - 95A 3 - 32A 3 - 84A 3 - 16
19A 3 - 81A 3 - 20A 3 - 9A 3 - 95A 3 - 37A 3 - 81A 3 - 20A 3 - 9A 3 - 95A 3 - 37
20A 3 - 12A 3 - 9A 3 - 80A 3 - 76A 3 - 52A 3 - 12A 3 - 9A 3 - 80A 3 - 76A 3 - 52
21A 3 - 6A 3 - 70A 3 - 56A 3 - 31A 3 - 74A 3 - 6A 3 - 70A 3 - 56A 3 - 31A 3 - 74
22A 3 - 43A 3 - 35A 3 - 21A 3 - 67A 3 - 21A 3 - 43A 3 - 35A 3 - 21A 3 - 67A 3 - 21
23A 3 - 78A 3 - 66A 3 - 34A 3 - 61A 3 - 60A 3 - 78A 3 - 66A 3 - 34A 3 - 61A 3 - 60
24A 3 - 81A 3 - 96A 3 - 42A 3 - 42A 3 - 1A 3 - 81A 3 - 96A 3 - 42A 3 - 42A 3 - 1
25A 3 - 5A 3 - 61A 3 - 13A 3 - 84A 3 - 18A 3 - 5A 3 - 61A 3 - 13A 3 - 84A 3 - 18
26
Sheet2 (2)
Cell Formulas
RangeFormula
I10:M25I10=VSTACK(B3:F6,I3:M4,P6:T15)
Dynamic array formulas.


VBA Code:
Sub Test_VSTACK()
  Dim vData As Variant
 
  vData = Evaluate("vstack(B3:F6,I3:M4,P6:T15)")
  Range("B10").Resize(UBound(vData, 1), UBound(vData, 2)).Value = vData
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,815
Messages
6,181,136
Members
453,021
Latest member
Justyna P

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