Filtering by using collection as criteria is practical ??

brkkrkmz112

New Member
Joined
Oct 24, 2016
Messages
43
Hi,

Im trying to use autofilter to filter in columns by using the values that are stored in an array or collection as criteria . I already know it is ok with array but when ım trying that with collection I couldnt achieve my aim so far and ım just wondering it is possible or not ? Im sharing just a simple example below.

Thanks in advance.

Code:
Sub collec()


Dim fruits As New Collection


fruits.Add "orange"
fruits.Add "apple"


With ActiveSheet


.AutoFilterMode = False


.Range("G3:G6").AutoFilter field:=1, criteria:=fruits, Operator:=xlFilterValues


End With


End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Hi,

I'm not sure you can retrieve all items for a collection as you want for filtering. You can retrieve item of a collection by index number, e.g.:

Code:
Sub collec01()
Dim fruits As New Collection
fruits.Add "orange"
fruits.Add "apple"
With ActiveSheet
    .AutoFilterMode = False
    .Range("g3:g6").AutoFilter field:=1, Criteria[COLOR=#ff0000]1[/COLOR]:=fruits[COLOR=#ff0000].Item(1)[/COLOR], Operator:=xlFilterValues
End With
End Sub

This code above will filter "orange" only. I'd use Dictionary object instead:

Code:
Sub collec02()
Dim D As Object, i As Long
Set D = CreateObject("scripting.dictionary")
D.Add "o", "orange"
D.Add "a", "apple"
With ActiveSheet
    .AutoFilterMode = False
    .Range("g3:g6").AutoFilter 1, Criteria1:=D.items, Operator:=xlFilterValues
End With
End Sub

Here is a useful article about Collection vs Dictionary:
Using the Dictionary Class in VBA

Regards,
 
Last edited:
Upvote 0
I'm thinking you could use Advanced Filter as its criteria is an Array.
 
Upvote 0
So ı understand from your answer that I couldnt use collection as I use array or dictionary. Even if I d like to use collection Im supposed to describe the item that stored in the collection one by one. By the way thanks for the link.


Hi,

I'm not sure you can retrieve all items for a collection as you want for filtering. You can retrieve item of a collection by index number, e.g.:

Code:
Sub collec01()
Dim fruits As New Collection
fruits.Add "orange"
fruits.Add "apple"
With ActiveSheet
    .AutoFilterMode = False
    .Range("g3:g6").AutoFilter field:=1, Criteria[COLOR=#ff0000]1[/COLOR]:=fruits[COLOR=#ff0000].Item(1)[/COLOR], Operator:=xlFilterValues
End With
End Sub

This code above will filter "orange" only. I'd use Dictionary object instead:

Code:
Sub collec02()
Dim D As Object, i As Long
Set D = CreateObject("scripting.dictionary")
D.Add "o", "orange"
D.Add "a", "apple"
With ActiveSheet
    .AutoFilterMode = False
    .Range("g3:g6").AutoFilter 1, Criteria1:=D.items, Operator:=xlFilterValues
End With
End Sub

Here is a useful article about Collection vs Dictionary:
Using the Dictionary Class in VBA

Regards,
 
Upvote 0
You need to use an for the criteria when you use the xlFilterValues operator.
 
Upvote 0
So ı understand from your answer that I couldnt use collection as I use array or dictionary. Even if I d like to use collection Im supposed to describe the item that stored in the collection one by one. By the way thanks for the link.
Exactly that's my view. You're welcome :-)
 
Upvote 0
You need to use an for the criteria when you use the xlFilterValues operator.
Hi Norie,
Thanks for jumping in. It seems that a word is missing from your comment after "an" isn't it?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
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