Collections instead of arrays

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,834
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have some data that consists of 10 columns.

I want to choose only those rows where columnd 10 is "Apples"

I can define an array consisting of a fixed number (10 in this case) columns and a variable number of rows, such as:

Code:
Dim OutputArray() As Variant
ReDim Preserve OutputArray(1 to Counter, 1 to 10) As Variant

However, I would like to do this using collections instead.

My problem is how can I define a collection to input 10 columns of data?

Thanks
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
You can add arrays to a collection:
Code:
    Dim c As Collection
    Dim n As Long
    
    Set c = New Collection
    
    c.Add Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), "item1"
    
    For n = LBound(c("item1")) To UBound(c("item1"))
        MsgBox c("item1")(n)
    Next n

BTW, your sample code wouldn't actually work since you can only resize the last dimension of an array if you use Preserve.
 
Upvote 0
Yes thanks for spotting my mistake but it was done quickly to illustrate the point.

In any case, I generally do not use ReDim Preserve, as I read somewhere it is "expensive".

Instead, I perfer to do two loops, one to find the dimension I want, then use ReDim.
 
Upvote 0

Forum statistics

Threads
1,215,785
Messages
6,126,887
Members
449,347
Latest member
Macro_learner

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