How to put data into an array?

excelos

Well-known Member
Joined
Sep 25, 2011
Messages
591
Office Version
  1. 365
Platform
  1. Windows
Hello

I want to create an array with dynamic data (that is not a simple range), what is the best way to do that?

E.g. how can I create an array with A1:A5,A10:A15 ?

From my reading, it seems there are few ways, like creating a SEQUENCE first etc but what is the optimal and most flexible way?

Thanks!
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
I don't know if it's the best, but you can copy the values to another cell and then you can take them as a sequence:

VBA Code:
Sub testarr()
  Dim a As Variant
  Range("A1:A5, A10:A15").Copy
  Range("C1").PasteSpecial xlPasteValues
  a = Range("C1", Range("C" & Rows.Count).End(3)).Value
End Sub
 
Upvote 0
Here's a way to put non-contiguous ranges into a vertical array and write the array to a column. The yellow-filled cells are the range myRng. myArr is written to col C.
Book1
ABCDEFGHIJ
1111161718191101
2121262728292102
31313
41414
51515
6110
7111
8112
9113
10110114
11111115
1211261
1311371
1411481
1511591
16101
1762
1872
1982
2092
21102
Sheet1
Cell Formulas
RangeFormula
F1:J2,A10:A15,A1:A5F1=COLUMN()&ROW()

VBA Code:
Sub MakeArray()
'create an array from a multi-area range object
'and write the array to a (vertical)column
Dim myRng As Range, myArr As Variant, i As Long, Ar As Range, j As Long, ct As Long
Set myRng = Range("A1:A5,A10:A15,F1:J2")
ReDim myArr(1 To myRng.Count, 1 To 1)
For i = 1 To myRng.Areas.Count
    For j = 1 To myRng.Areas(i).Count
        ct = ct + 1
        myArr(ct, 1) = myRng.Areas(i).Cells(j).Value
    Next j
Next i
Range("C1:C" & UBound(myArr, 1)).Value = myArr
End Sub
 
Upvote 0
Hi @JoeMo, I was looking for a way to not read cell by cell, because that would make the process very slow, depending on the number of cells, of course. But, either way, it could be simplified into a single loop:

VBA Code:
Sub testarr_3()
  Dim rng As Range, c As Range, a As Variant, n As Long
  Set rng = Range("A1:A5, A10:A15, F1:J2")
  ReDim a(1 To rng.Count, 1 To 1)
  
  For Each c In rng
    n = n + 1
    a(n, 1) = c.Value
  Next
  Range("C1").Resize(UBound(a)).Value = a
End Sub
 
Upvote 0
Hi @JoeMo, I was looking for a way to not read cell by cell, because that would make the process very slow, depending on the number of cells, of course. But, either way, it could be simplified into a single loop:

VBA Code:
Sub testarr_3()
  Dim rng As Range, c As Range, a As Variant, n As Long
  Set rng = Range("A1:A5, A10:A15, F1:J2")
  ReDim a(1 To rng.Count, 1 To 1)
 
  For Each c In rng
    n = n + 1
    a(n, 1) = c.Value
  Next
  Range("C1").Resize(UBound(a)).Value = a
End Sub
Good point Dante. I was trying to emphasize the multi-area-range aspect the OP seemed concerned about.
 
Upvote 0
It depends on what sort of data you have in the range (text, numerical etc) and what you want to subsequently do with the array values but if your range of interest is going to consist of two 'areas' you can get the values (as strings) into an array without looping like this*.

Assumes "|" is not part of the data
VBA Code:
Dim a As Variant
Dim myRng As Range

Set myRng = Range("A1:A5,A10:A15")
a = Split(Application.TextJoin("|", 0, myRng.Areas(1), myRng.Areas(2)), "|")

If the number of areas is unknown then this at least only requires looping each area, not each cell.

VBA Code:
Dim a As Variant
Dim myRng As Range, rA As Range
Dim s As String

Set myRng = Range("A1:A5,A10:A15")
For Each rA In myRng.Areas
  s = s & "|" & Application.TextJoin("|", 0, rA.Value)
Next rA
a = Split(Mid(s, 2), "|")

* Caveat with both codes is that the ranges and/or the values in them are not so big as to exceed the TEXTJOIN character limit of 32,767
 
Upvote 0
Sorry forgot to mention, no VBA please. Any formula way? @JoeMo thanks will dive into that!
 
Upvote 0
Sorry forgot to mention, no VBA please. Any formula way?
In that case, what exactly do you mean by "put into an array"?
Tell (&/or show with XL2BB) us more about exactly what you have and what you are trying to end up with.
 
Upvote 0
Taking another guess:

excelos_1.xlsm
ABC
1aa
2bb
3cc
4dd
5ee
6fj
7gk
8hl
9im
10jn
11ko
12l
13m
14n
15o
Sheet1
Cell Formulas
RangeFormula
C1:C11C1=FILTER(A1:A15,(ROW(A1:A15)<=5)+(ROW(A1:A15)>=10))
Dynamic array formulas.
 
Upvote 0
This collect A1:A5 and A11:A15 into a 2D array
=CHOOSE({1,2},A1:A5,A11:A15)
Then, for example, SUM:
=SUM(CHOOSE({1,2},A1:A5,A11:A15))
Book1
ABCDEF
11#VALUE!{1,7;2,8;3,9;4,10;5,11}
2260
33
44
55
6
7
8
9
10
117
128
139
1410
1511
Sheet1
Cell Formulas
RangeFormula
D1D1=CHOOSE({1,2},A1:A5,A11:A15)
D2D2=SUM(CHOOSE({1,2},A1:A5,A11:A15))
 
Upvote 0

Forum statistics

Threads
1,215,546
Messages
6,125,459
Members
449,228
Latest member
moaz_cma

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