Dynamic Named Range as Array for AutoFilter not working

shella

New Member
Joined
Jan 15, 2014
Messages
34
Hey All! I've been searching the forums in vain and am hoping someone out there can help. I have a dynamic named range ("FilterCriteria") and am trying to use it as an array for autofilter. I'd like to see if any of the "FilterCriteria" values appear in each cell in Column H. H is a helper column that is concatenating multiple other columns, as my filter variables exist in several different columns. I can't seem to get the below code to work, and imagine there might even be a better way?

Thank you so much!

Code:
Sub RunFilter_New()
Dim vCrit As Variant


'Need Help: Trying to see if any value in my "FilterCriteria" named range
'appears in Column H on "Data" worksheet (by row)
'I'm using H as a helper column since my filter criteria span multiple columns (A, E, F, G)
'If it appears in the H cell - in any part of the cell, autofilter those rows
'If the value does not appear in the H cell, hide that row


vCrit = Worksheets("KEEP-unique").Range("FilterCriteria").Value


Worksheets("Data").Range("$A$4").CurrentRegion.AutoFilter _
    Field:=8, _
    Criteria1:="*" & Application.Transpose(vCrit) & "*", _
    Operator:=xlFilterValues


End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Re: Dynamic Named Range as Array for AutoFilter not working - please HELP!

AutoFiltering for cells that "Contain" certain text is restricted to 2 such "contains" values. For what you are trying to do, as I understand it, could possibly be done with AutoFilter via a work-around, but more directly with Advanced Filter instead.
Try this with a copy of your workbook.

In 'KEEP-unique' column J (or choose another available column) leave row 1 blank and in row 2 (ie in J2) put the formula
=LOOKUP(9.99E+307,SEARCH(FilterCriteria,Data!H5))

Now try this code (edit code if you didn't use column J above)
Code:
Sub FilterManyContains()
  Sheets("Data").Range("A4").CurrentRegion.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Sheets("KEEP-unique").Range("J1:J2"), Unique:=False
End Sub

To unfilter, use 'Clear' in the 'Sort & Filter' group on the 'Data' ribbon tab or we could have another code to do that.

If it is a 'must' to use Autofilter, post back.
 
Last edited:
Upvote 0
Re: Dynamic Named Range as Array for AutoFilter not working - please HELP!

Peter,

Thanks for your suggestions but I couldn't get it to filter appropriately. I'm wondering if I strip back my request to a basic description, if it will widen the opportunity for additional vba possibilities...?

I'm trying to see if ANY words from column "X" (ex. "apple" or "pear" or "orange" etc.) appear in a range on another worksheet (ex. "A4:J100") then autofilter to show only those rows...

Is what I'm asking within the realm of possible? Or do I need to consider a different approach?

Thank you so much for your time and expertise!
 
Upvote 0
Re: Dynamic Named Range as Array for AutoFilter not working - please HELP!

In the other worksheet, can apple/pear/orange appear in any of the 10 column A:J? The code you posted above, was only looking in column H.

What about we just work with a single sheet to start with and you make a small set of sample dummy data with those three words in column X and 6 or 8 rows of data in columns A:J.
Post that sample data (my signature block below has help with that if needed) & explain which rows should be visible after the filter is applied.

One further question: If "apple" is one of the words in column X and "pineapple" is one of the words in a column you are trying to filter, should the pineapple row be visible (as it would normally be if you used *apple* in AutoFilter like in the code you posted) or hidden (since it doesn't contain the exact word apple)?
 
Upvote 0
Re: Dynamic Named Range as Array for AutoFilter not working - please HELP!

Peter,

Thank you for your questions - particularly regarding Column H, as I should've explained myself in the initial post. Attached is the single sheet file, as suggested, with data in Columns A:J (partially populated) and Filter Criteria in Column "X". Answers to your questions are below:

1. The filter criteria can appear in any of the following columns (as of current build): A, E, F, or G. I was using H as a helper column because I believed (erroneously, I hope) that all possible filters had to reside in a single column for autofilter to work. As such, we can disregard Column H.

2. As of right now, the goal would be an exact match (apple = apple, apple <>= pineapple - hiding pineapple results). That said, I'm wondering if you would be so generous as to share with me both the exact match and the wildcard option, as I envision future iterations may want to use that capability and I'd like to learn how to code that!


Excel 2016 (Windows) 32 bit
ABCDEFGHIJKLMNOPQRSTUVWX
1DATAFilter Criteria
2Risk/Concern
3SourceNameIntel Description (or Question)Intel (or Answer)TagTag 2Tag 3Helper ColumnSOW/PPW SectionProp SectionMetric
4Customer Engagement1Risk/ConcernCustomer Engagement Risk/Concern Hot Button - Hope
5Competitive Intel1MetricCompetitive Intel Metric Hot Button - Fear
6Black Hat1MetricMetricBlack Hat Metric Metric Ghost
7Industry Day1Hot Button - HopeIndustry Day Hot Button - Hope Defend
8SME Interview1Hot Button - FearSME Interview Hot Button - Fear Kudo
9Teammate Data Call1GhostHot Button - HopeTeammate Data Call Ghost Hot Button - Hope Proof
10Solutioning1DefendSolutioning Defend Win Theme
11Other1KudoOther Kudo Past Performance
12Customer Engagement1MetricCustomer Engagement Metric Discriminator
13Competitive Intel1ProofCompetitive Intel Proof Graphic
14Black Hat1Win ThemeGhostBlack Hat Win Theme GhostSolution Element
15Industry Day1Past PerformanceDefendIndustry Day Past Performance DefendCustomer Engagement
16SME Interview1DiscriminatorGhostKudoSME Interview Discriminator Ghost KudoCompetitive Intel
17Teammate Data Call1GraphicDefendMetricTeammate Data Call Graphic Defend MetricBlack Hat
18Solutioning1Solution ElementKudoProofSolutioning Solution Element Kudo ProofIndustry Day
19Other1KudoMetricWin ThemeOther Kudo Metric Win ThemeSME Interview
20Industry Day1MetricIndustry Day Metric Teammate Data Call
21SME Interview1ProofSME Interview Proof Solutioning
22Teammate Data Call1Solution ElementTeammate Data Call Solution Element Other
23Solutioning1MetricSolutioning Metric
24
25

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Data

Thank you so very much for your time and brainpower!

Best,
Shella
 
Upvote 0
Re: Dynamic Named Range as Array for AutoFilter not working - please HELP!

I'm not sure how good/representative that sample is? By my reckoning every row of the data contains one of the filter criteria so filtering would not hide any of the rows. Is that correct?
 
Upvote 0
Re: Dynamic Named Range as Array for AutoFilter not working - please HELP!

Peter,

It actually is intended. I'm using just a skeleton dataset to set up the table with macros to perform how I want it to perform before building out. It will have a button for each filter criteria that will show those rows associated with that criteria (ex. "Apple" shows apple, and if both "Apple" and "Orange" are selected both of those rows will show). Does that make sense?

Thanks!

Shella
 
Upvote 0
Re: Dynamic Named Range as Array for AutoFilter not working - please HELP!

Does that make sense?
I understand what you are saying (I think) but when testing a filtering routine, it makes sense to me that some rows should end up hidden and some visible. If they all remain visible, you don't actually know if your code has done anything. :)

Therefore I have altered your sample considerably for testing and posting back here.
- I have removed the helper column that you didn't really want anyway. However I inserted a new column to keep the FilterCriteria in column X.
- I have re-defined FilterCriteria to refer to X2:X6 (The FilterCriteria range must have no blank cells in it)
- I have included the apple/pineapple example to demonstrate clearly (I hope) the two versions of the code you requested.
- Note that I have hidden many of the columns to keep my screen shot smaller.
- Range Y1:Y2 is also used (briefly) as a helper in my code.

A) To filter as you were filtering with that helper column (where pineapple would show up even if the filter criteria included only apple), use the code as below. I have marked yellow in column R the rows that remained visible after that filter.

B) To filter for rows that contain cells exactly as per FilterCriteria (pineapple would be hidden), swap to the alternate line in the code that is currently commented out. I have marked blue in column S the rows that remained visible after that filter.

Rich (BB code):
Sub Adv_Filter()
  Dim rCrit As Range
  
  Set rCrit = Range("Y1:Y2")
  
  rCrit.Cells(2).Formula = "=LOOKUP(9.99E+307,SEARCH(FilterCriteria,TEXTJOIN("" "",TRUE,A4,E4:G4)))"
'  rCrit.Cells(2).Formula = "=LOOKUP(9.99E+307,SEARCH(""|""&FilterCriteria&""|"",""|""&TEXTJOIN(""|"",TRUE,A4,E4:G4)&""|""))"

  Range("A3").CurrentRegion.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=rCrit, Unique:=False
  rCrit.ClearContents
End Sub

To re-display all the rows, click 'Clear' in the 'Sort & Filter' group on the 'Data' ribbon tab or run this code:
Rich (BB code):
Sub Clear_Adv_Filter()
  If ActiveSheet.FilterMode Then ActiveSheet.ShowAllData
End Sub

Excel Workbook
AEFGHIJRSTUXY
1DATAFilter Criteria
2Risk/Concern
3SourceTagTag 2Tag 3SOW/PPW SectionProp SectionMetric
4Customer EngagementRisk/ConcernsHot Button - Hope
5Competitive IntelMetricapple
6applexxxSolution
7pineapplexxx
8SME InterviewHot Button - Fear
9Teammate Data CallGhostHot Button - Hope
10SolutioningDefend
11OtherKudo
12Customer EngagementMetrical
13Competitive IntelProof
14Black HatWin ThemeGhost
15Industry DayPast PerformanceDefend
16SME InterviewDiscriminatorGhostKudo
17Teammate Data CallGraphicDefendMetric
18SolutioningSolution ElementKudoProof
19OtherKudoMetricWin Theme
20Industry DayMetric
21SME InterviewProof
22Teammate Data CallSolution Element
23SolutioningMetric
24
Data
 
Last edited:
Upvote 0
Re: Dynamic Named Range as Array for AutoFilter not working - please HELP!

Peter,

You, sir, are a gentleman and a scholar! Thank you 1000x over for seeing past my inept request (dataset with filter criteria in each row) and providing both versions of code that work brilliantly!! This has been an intractable problem for me, and I'm so grateful for your help!

Because I like to learn about the actual code to avoid having to ask for help in the same area, could you perhaps share with me why that autofilter line works or point me in the right direction for a google search that would allow me to read up on it/learn more about it?

Thank you again SO very much!!
 
Upvote 0
Re: Dynamic Named Range as Array for AutoFilter not working - please HELP!

Also, is there a way I can/should mark this post {SOLVED} ? Thank you again!!
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,738
Members
448,988
Latest member
BB_Unlv

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