Excel - Can Advanced Filter Criteria Be in The VBA Rather Than A Range? - Stack Overflow
Excel - Can Advanced Filter Criteria Be in The VBA Rather Than A Range? - Stack Overflow
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
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
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)
Related
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?
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
Add a comment
Your Answer
Links Images Styling/Headers Lists Blockquotes Code HTML Tables Advanced help
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