Populate multiple Variant arrays with loop and collection

GeeWhiz7

Board Regular
Joined
Nov 22, 2021
Messages
214
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi folks,
I am trying to figure out how I can use a loop to
1) establish a number of arrays in a collection and 2) populate those individual arrays inside the loop.

Code I am trying to figure out below and a sample mini-sheet at the bottom.

VBA Code:
Sub MakeAndFillArrays()
'#########
'Code should make multiple 2-D arrays inside a collection (in this case and
'then fill each of them with data from a table starting at the bottom and going up by 5 rows at a time
'each row of data gets put in a separate array in the collection
    
    Dim InSh, OutSh As Worksheet
    Dim lr, lc, i As Long
    
    Set InSh = Worksheets("Sheet1")
    Set OutSh = Worksheets("Sheet2")
    
    With InSh
        lr = .Cells(Rows.Count, 1).End(xlUp).Row
        lc = .Cells(1, Columns.Count).End(xlToLeft).Column
    End With
  
    OutSh.Cells.Clear
        
        Dim col As New Collection
        Dim a As Variant
        Dim n As Integer

            For n = 1 To 9
                a = Array()
                ReDim a(1 To n, 1 To lc)
                col.Add a
'             '####Now I would like to populate the arrays, possibly as they are being created one by one.
'             '####If I remove the line below from computation then the arrays all get set up, but I can't figure out how to populate them.
'             '####If I keep the line then it only ends up with one array in the collection with the last step (n=9) data.
               a = (.Range(.Cells(lr + 1 - 3, 1), .Cells(lr + 1 - 3, lc)).Value)  
            Next

End Sub

Book2
ABCDEFGHIJKLMN
1NumberABCDE
21-44-4847-214
32-4717-453-5
43-3119-98-13
54-974543-33
65-32-39-18-131
76-4515224-9
87-23-2529141
98326-33021
10949-33-50-37-30
1110-2212-84726
1211-18-2230-2-31
131234-29-4632-3
14133238-3547-4
1514-19-1550-25-35
161520149-5-45Array(9)
1716-474-27-321
1817-35-3613-2318
1918-23-23-9-41Array(8)
201928-14-17643
2120-5-291146-35
2221462818-20-26Array(7)
2322-4221-20831
2423-44-39-143815
25249-3230-48-24Array(6)
262514402025-4
272622-3341148
2827-7-3918-2221Array(5)
2928-21-517-7-32
3029222-304627
3130-13-183713-16Array(4)
323128-28-36103
3332-43-420-191
3433-3-37-85031Array(3)
3534-19181619-14
3635-3632-46-3433
37361-1628-1835Array(2)
38373529133924
3938634-23-48-30
4040-10-44334010Array(1)Starts from bottom, step by 3, stops at 9 arrays
Sheet1
Cell Formulas
RangeFormula
A3:A39A3=A2+1
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Does this do what you need ?

VBA Code:
            For n = lr To 16 Step -3
                With InSh
                    a = (.Range(.Cells(n, 1), .Cells(n, lc)).Value)
                End With
                col.Add a
            Next
 
Upvote 0
Solution
Does this do what you need ?

VBA Code:
            For n = lr To 16 Step -3
                With InSh
                    a = (.Range(.Cells(n, 1), .Cells(n, lc)).Value)
                End With
                col.Add a
            Next
Hi Alex, sorry for delay getting back. This still ends up with only one final array (at n = 16) instead of a(1), a(2), a(3) etc being separate. It correctly steps up from the bottom by -3 and the locals window shows each step has the correct values, but each new iteration is overwriting the previous array values.
 
Upvote 0
You might need to clarify what you are trying to do.
Put the collection in the watch window and you should find 9 items each with 1 row in an array.
I have logged off for the night.
 
Upvote 0
You might need to clarify what you are trying to do.
Put the collection in the watch window and you should find 9 items each with 1 row in an array.
I have logged off for the night.
No worries and thanks for the help. I was able to get your suggestion to work. The main reason I was curious to use a collection instead of the outright array (and just count back rows there) was the ability to add/remove so this helps me with that.
 
Upvote 0
Thanks for the follow up. I was going to suggest looking into using Dictionaries but they don't work on MacOS.
You don't see collections used that much but they can be useful when you don't know how many items you are going to add in advance.
It doesn't have the "exists" method that a dictionary has but there is a workaround for that.
Post another thread if you decide you want to pursue it further.
 
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,431
Members
452,326
Latest member
johnshaji

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