0% found this document useful (0 votes)
225 views1 page

Excel - Can Advanced Filter Criteria Be in The VBA Rather Than A Range? - Stack Overflow

Advanced Filter Criteria as VBA Array and not a range

Uploaded by

vaskore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
225 views1 page

Excel - Can Advanced Filter Criteria Be in The VBA Rather Than A Range? - Stack Overflow

Advanced Filter Criteria as VBA Array and not a range

Uploaded by

vaskore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 1

1

Products Search… 1 1

Home
Can Advanced Filter criteria be in the VBA rather than a range? Ask Question

PUBLIC Asked 5 years, 3 months ago Active 5 years, 3 months ago Viewed 4k times

Questions
After trying in vain to set more than 2 criteria in a normal AutoFilter fashion via VBA, I have come to
Tags The Overflow Blog
learn that it must be done via advanced filter.
Users 4 How often do people actually copy and
offending example: paste from Stack Overflow? Now we
FIND A JOB
know.
Jobs Worksheets(1).Range("A1").AutoFilter Field:=ColNum, Criteria1:="A*", Operator:=xlOr, Criteria2:="B*",
Podcast 331:Operator:=xlOr,
One in four visitors toCriteria3:="C
Stack
Companies Overflow copies code

I am hoping to pass the criteria through to a function (rather than a macro) from a PowerShell
TEAMS Featured on Meta
script. I have it all working fine and as expected for 1 criteria, but now I'd like 3.
Stack Overflow for Stack Overflow for Teams is now free for
Teams – Collaborate
I suppose I could instead write a macro to insert a new sheet, write in the criteria then filter on that
up to 50 users, forever
and share knowledge new range but I'd rather check the preferred way first.
with a private group. New onboarding for review queues

vba excel dictionary autofilter Should the [complete] tag be removed?

Outdated Answers: results from use-case


Share Edit Follow edited Jan 16 '16 at 18:07 asked Dec 30 '15 at 15:24 survey
user4039065 Chris Downvotes Survey results
194 3 21

Create a free Team Hot Meta Posts

What is Teams? Can't be anything but a range I'm afraid. – Rory Dec 30 '15 at 15:36
13 Why was this answer to “numpy-like-
1 Create a variant array of wildcard matches and then use the array of full values with the standard package-for-node” deleted?
AutoFilter method. I've done this with a dictionary object to use its unique index property. – user4039065
Dec 30 '15 at 15:37
Linked
Won't this become a custom filter and fail due to >2? – Chris Dec 30 '15 at 15:43

@Chris - see my generic solution below. – user4039065 Dec 30 '15 at 16:08 3 Auto Filter in VBA
1

9 Set Auto Filtering multiple wildcards


Add a comment

2 Can AutoFilter take both inclusive and non-


1 Answer Active Oldest Votes inclusive wildcards from Dictionary keys?

2 Optimizing VBA / Excel Macro Code


(Finding Duplicates in Large Sheet)
To filter on multiple wildcards, create a variant array of wildcard matches and then use the array of
full values with the standard AutoFilter method. You can minimize the array by putting a dictionary 1 Auto Filter sort in VBA

6 object to use with its unique index property. 2 Simulate AdvancedFilter CriteriaRange
Object through array
Consider the following sample data.
1 AdvancedFilter to filter one variable at the
same time in three columns (vba)

0 Optimal data manipulation excel

Related

1 Find all combinations of advanced filter


criteria

1 Excel is copying all data even if the


Autofilter criteria matches no data entry

1 Filter Range by specific Color Index/RGB


using
Worksheet.Range.Autofilter(Field,Criteria,O
perator)

0 Use Range of Advanced Filter Cells as


Criteria for AutoFilter on Another Sheet
Run this code.
3 How to filter in VBA for ''ő'' character?

Sub multiWildcards() 2 excel: run time error 1004 in advanced filter


Dim v As Long, vVALs As Variant, dVALs As Object
Dim colNum As Long 2 How to specify a Column for VBA's
Autofilter function by name rather than by
Set dVALs = CreateObject("Scripting.Dictionary") its index?
dVALs.comparemode = vbTextCompare
colNum = 2 'column B 0 Excel - Advanced Filter: Filter values
greater than today
With Worksheets(1)
If .AutoFilterMode Then .AutoFilterMode = False Hot Network Questions
With .Cells(1, 1).CurrentRegion
vVALs = .Columns(colNum).Cells.Value2 Does the "Spirit of God" (through Jude) in Jude
For v = LBound(vVALs, 1) To UBound(vVALs, 1) 1:14 validate the book of Enoch?
If Not dVALs.exists(vVALs(v, 1)) Then
Select Case UCase(Left(vVALs(v, 1), 1)) What are the advantages of using DC electric
power over AC, as e.g. done in the EMB 135/145?
Case "A", "B", "C"
dVALs.Add Key:=vVALs(v, 1), Item:=vVALs(v, 1) Is OPTION (RECOMPILE) used in production
Case Else environments?
'do nothing
Draw the flag of Bangladesh
End Select
End If Could Derek Chauvin be retried?
Next v
How to deal with professors who don't follow the
text
If CBool(dVALs.Count) Then
'populated the dictionary; now use the keys How can one wish someone a "good night" when
.AutoFilter Field:=colNum, Criteria1:=dVALs.keys, Operator:=xlFilterValues they sleep during the day?
Else
Are All the Items the Same?
Debug.Print "Nothing to filter on; dictionary is empty"
End If Some doubts about symbolic equations in Wolfram
Mathematica
'.CurrentRegion is now filtered on A*, B*, C* in column B
Why does trying to break into the NT 3.1 kernel
'do something with it reboot my 486DX4 machine?
End With
End With Weird error in typing latex eqn

How to detach a cleat with a warped screw


dVALs.RemoveAll: Set dVALs = Nothing
Results
End Subshould be: How to preserve undeveloped film as a legacy gift

What is the red barb for in the NOAA weather


briefing?

What is super-karate in the DC comics?

Why would The Machines not hunt humanity?

"gerund + genitive" vs "gerund+accusative"


("scribendo epistulas" vs "scribendo epistularum")

What is a common idiom meaning, 'that a situation


These results can be duplicated with many other wildcard scenarios. The Select Case statement is is likely to change all of a sudden without notice or
ideal as it supports the Like keyword for building your collection of matches. By starting with a reason'?

value dump into a regular variant array, cycling through even large rows of data can be done Someone asked me to login to his bank account
and make transfers to another account
quickly.
How do I make my soup a more appealing colour?

Share Edit Follow edited Jun 20 '20 at 9:12 answered Dec 30 '15 at 16:08 Can I use a single 20A circuit for two bathrooms?
Community ♦ user4039065
Can a wild shaped character use the medicine skill
1 1 to stabilize a dying creature?

Most effective way to improve cantilever brakes


Interesting, will give it a go. Thanks very much. Next step is passing the array of values to VBA from
PowerShell! – Chris Dec 30 '15 at 16:12 Can I ask for documentation for what I'll be
working on before starting a new job?
Just tried to run this and get type mismatch on .AutoFilter Field:=colNum, Criteria1:=dVALs.keys,
Operator:=xlFilterValues – Chris Dec 30 '15 at 16:18 Question feed
This may have something to do with your version of Excel and its SP. Try Criteria1:=
(dVALs.keys) . I honestly did not make the image up. – user4039065 Dec 30 '15 at 16:19

I wouldn't dare suggest you did! It's likely me making some mistake. I've added the brackets but get the
same error. I'm using Excel 2013 if it helps – Chris Dec 30 '15 at 16:23

Could you try this sample workbook? It is a small XLSB. – user4039065 Dec 30 '15 at 16:25

If the dictionary is empty, you will receive that error. See my error control addition above. – user4039065
Dec 30 '15 at 16:56

Apologies for the slow reply, thank you very much for the further help. Will give it a test this morning. –
Chris Jan 4 '16 at 8:53

I now get no error but nothing happens, I've tried updating the cases to my actual values to be filtered on,
including and not including wildcard. I remember you mentioning the Like keyword but don't see it used
anywhere, is this intended? I'm unable to use your test spreadsheet as dropbox is not available on this
network. I run all my macros from a personal XLSB, is this sufficient? The autofilter is removed so I know
the macro is being run. – Chris Jan 4 '16 at 9:06

I can confirm the issue now is the dictionary is empty, wasn't sure where to see the debug output so I
added a MsgBox and it appears. – Chris Jan 4 '16 at 9:32

Ignore the above - got it working. Absolutely a case of user not being clear of requirements! Once I
properly went through your code I realized it was selecting on 1 letter of the value (from the A,B,C
example). My actual scenario needs to filter on the first 3, so I updated the "Select Case" line and it works
perfectly. Thank you so much for your time and effort. – Chris Jan 4 '16 at 10:02

Glad you got that working. It's a powerful tool in just a handful of code lines. – user4039065 Jan 4 '16 at
10:45

Absolutely, I've put in a "sub" select to try on lengths of 2 as well which works! I want to extend it now to
take an array of values, but I'll give that a go and create a separate question in the future if I get stuck.
Thanks again – Chris Jan 4 '16 at 11:03

1 btw, Debug.Print reports to the VBE's Immediate window. Tap Ctrl+G to reveal it (alternately View ►
Immediate Window). – user4039065 Jan 4 '16 at 11:07

is there a way to specifically exclude a wildcard? For example I want to include "A*" and "B*", but exclude
"BB*"? – Chris Jan 5 '16 at 13:21

After meeting the B requirement, you could easily add a line like If Not Left(vVALs(v, 1), 2) =
"BB" Then dVALs.Add Key:=... . If there were many of these, it might make sense to assign
vVALs(v, 1) to a string variable just above the Select Case ands use the string var in all
subsequent conditions. – user4039065 Jan 5 '16 at 13:44

Hmm ok, I need to make it a bit more flexible. This has so many comments now and I've altered the
macro so it might be better to make a new question. – Chris Jan 5 '16 at 14:06

Drop one more note here when you do and I'll try to have a look. – user4039065 Jan 5 '16 at 14:14

New question here stackoverflow.com/questions/34614417/… – Chris Jan 5 '16 at 14:46

Add a comment

Your Answer

Links Images Styling/Headers Lists Blockquotes Code HTML Tables Advanced help

Post Your Answer

Not the answer you're looking for? Browse other questions tagged vba excel dictionary autofilter or
ask your own question.

STACK OVERFLOW PRODUCTS COMPANY STACK EXCHANGE Blog Facebook Twitter LinkedIn Instagram
NETWORK
Questions Teams About
Technology
Jobs Talent Press
Life / Arts
Developer Jobs Directory Advertising Work Here
Culture / Recreation
Salary Calculator Enterprise Legal
Science
Help Privacy Policy
Other
Mobile Terms of Service
Disable Responsiveness Contact Us
Cookie Settings
site design / logo © 2021 Stack Exchange Inc; user contributions
Cookie Policy licensed under cc by-sa. rev 2021.4.20.39115

You might also like