VBA: Advanced Filter Limitations

zero269

Active Member
Joined
Jan 16, 2023
Messages
253
Office Version
  1. 365
Platform
  1. Windows
Hello,

I just spent a great deal of time getting a table to filter based on a list of criteria. However, once the filter is applied, I lose all ability to apply any manual sorting and/or filtering as needed.
VBA Code:
'Filter Active Series
Range("t_" & wsName & "[#All]").AdvancedFilter Action:=xlFilterInPlace, _
    CriteriaRange:=SeriesRange, Unique:=False
The CriteriaRange points to a Named Range on a "List" sheet using the following formula… as recommended everywhere I looked on this topic.
Excel Formula:
=SUBSTITUTE(CHOOSECOLS(FILTER(t_AuthorSeries,((t_AuthorSeries[Status]="active")*((t_AuthorSeries[Student]="JohnDoe")+(t_AuthorSeries[Student]="All")))),1),";","")
I tried using each of the following - separately - to restore the filter drop downs to no avail:
VBA Code:
'Reenable AutoFilter
Range("t_" & wsName).AutoFilter 'option 1 cleared filtered range
Range("t_" & wsName).ShowAutoFilterDropDown = True 'option 2 does nothing
I assume this is how the Advanced Filtering works? It's a one off that prevents any subsequent manual sorting and/or filtering?

So, I think my question is two-part:
  1. Am I going about this the wrong way where I lose subsequent manual sorting/filtering?
  2. I need to use a different approach?
Ultimately I'm looking to automate the same actions as filtering a column by selecting those checkboxes.

Many of the books have more than one criteria in the Series - separated by semicolons - which means I need to filter using wildcards if using vba.
For example, if one of the Series in the SeriesRange list is the "Berenstain Bears", I would need to check all of the following:
  • Berenstain Bears;
  • Berenstain Bears; Bright & Early Book;
  • Berenstain Bears; Bright & Early Book; Berenstain Bear Scouts;
  • Berenstain Bears Big Chapter Books;
  • Living Lights; Berenstain Bears;
  • First Time Books; Berenstain Bears;
  • I Can Read! (HarperCollins): Level 1; Berenstain Bears;
  • Step into Reading: Step 3; Berenstain Bears;
For reference, here's my full sub procedure:
VBA Code:
Sub Filter_ActiveSeries()

    Application.ScreenUpdating = False
    
    'Data Types/Declarations
    Dim Student As String
    Dim wsName As String: wsName = ActiveSheet.Name

    'Verify Sheet Name
    Select Case wsName
        Case "Books" 'prompt for Student Name
        
            'Data Types
            Dim Arr As Variant, Titlebar As String, i As Long, YNAnswer As Integer
        
            'Declarations
            Arr = Range("t_Students[Name]").Value
            Titlebar = "Choose Student..."
        
            'Build Message Box
            Dim NewLine As String, Message As Variant
            For i = LBound(Arr, 1) To UBound(Arr, 1)
                NewLine = i & vbTab & Arr(i, 1) & vbNewLine
                Message = Message + NewLine
            Next i
            
            On Error GoTo ErrHandler
            
            'Input Prompt
            Dim Choice As Integer: Choice = InputBox(Message, Titlebar)
            If Choice >= LBound(Arr, 1) And Choice <= UBound(Arr, 1) Then 'If Valid Entry
                Student = Arr(Choice, 1)
            End If
        
        Case Else: Exit Sub
    
    End Select
    
    'Set Active Worksheet by Name
    Dim ws As Worksheet: Set ws = Worksheets(wsName)
    
    'Declarations
    Dim SeriesRange As Range
    Set SeriesRange = Range("l_Lists_" & Student & "_Series")
    Set SeriesRange = SeriesRange.Offset(-1, 0).Resize(SeriesRange.Rows.Count + 1, SeriesRange.Columns.Count)
    
    'Clear any active filters
    If ws.FilterMode Then ws.ShowAllData
    
    'Filter Active Series
    Range("t_" & wsName & "[#All]").AdvancedFilter Action:=xlFilterInPlace, _
        CriteriaRange:=SeriesRange, Unique:=False
    
    'Re-enable Filter Drop Downs
'    Range("t_" & wsName).AutoFilter    'it's clearing the advanced filter
'    Range("t_" & wsName).ShowAutoFilterDropDown = True 'no action
    
    Application.ScreenUpdating = True

ErrHandler:     'Exit on error

End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
UPDATE:

I recorded a macro earlier where I randomly selected items from the list. I see the Criteria is using an Array like so...
VBA Code:
ActiveSheet.ListObjects("t_Books").Range.AutoFilter Field:=5, Criteria1:= _
        Array("A Math Reader; Step into Reading: Step 1;", _
        "Adventures of Wedgieman; Step into Reading: Step 3; Wedgieman;", _
        "Amelia Bedelia; Step into Reading: Step 1;", _
        "Barbie I Can Be...; Step into Reading: Step 1;", _
        "Barbie I Can Be...; Step into Reading: Step 2;", _
        "Barbie: Mariposa & the Fairy Princess; Step into Reading: Step 2;", _
        "Barbie; Step into Reading", "Barbie; Step into Reading: Step 1;", _
        "Barbie; Step into Reading: Step 2;", _
        "DC Super Friends (Random House); Step into Reading: Step 2;", _
        "Disney * Pixar Cars; Step into Reading: Step 1;", _
        "Disney * Pixar Cars; Step into Reading: Step 3;", _
        "Disney * Pixar Finding Dory; Step into Reading: Step 2;", _
        "Disney Frozen; Step into Reading: Step 1;", _
        "Disney Frozen; Step into Reading: Step 2;", _
        "Disney Princess (Random); Step into Reading: Step 1;", _
        "Disney Princess (Random); Step into Reading: Step 2;", _
        "Disney Princess: Palace Pets; Step into Reading: Step 1;", _
        "Disney Princess; Step into Reading: Step 1; Step into Reading: Step 2;", _
        "Disney Princess; Step into Reading: Step 2;", _
        "Disney Raya and the Last Dragon; Step into Reading: Step 1;", _
        "Disney/Pixar Soul; Step into Reading;", "Dr. Seuss; Step into Reading: Step 1;" _
        , "DreamWorks Trolls World Tour; Step into Reading: Step 2;", _
        "Iron Man: Armored Adventures; Step into Reading: Step 3;"), Operator:= _
        xlFilterValues
When I run the macro against my dataset, it applies the filter as if done maually... thus not losing manual or automated subsequent sorting/filtering functionality. (y)

However, I now need to figure out how to create this Array using the full contents of the Series values based on my active series list...

Example:
Full cell value = Amelia Bedelia; Step into Reading: Step 1;
Search value = * Amelia Bedelia *

I'll keep playing around with the filter function to see if I can produce the same results.
I'm currently playing around with a formula I'm using in a helper column for the time being:
Excel Formula:
=IF(OR(COUNTIF([@Series],"*"&l_Lists_JohnDoe_Series&"*")),[@Series],"")
 
Upvote 0
UPDATE 2:

I was able to repurpose a formula I got help with from @Alex Blakenburg from this post:
I just had to add UNIQUE and limit the range to the Books table Series column.
Excel Formula:
=LET(x,UNIQUE(FILTER(t_Books[Series],BYROW(--ISNUMBER(SEARCH(TOROW(l_Lists_JohnDoe_Series),t_Books[Series])),LAMBDA(r,SUM(r ))))),SORT(x,1))
Now I just need to get this work in VBA to create my Array Criteria...

Bold text are the values from the search criteria...
A portion of the results:

50 States to Celebrate; Green Light Readers: Level 3;
Adventures in Odyssey; Imagination Station; Focus on the Family;
Bad Kitty (Chapter Books);
Beginner Books; Bright & Early Book; Dr. Seuss; Cat in the Hat's Learning Library;
Beginner Books; Cat in the Hat's Learning Library;
Cat in the Hat Knows a Lot About That!; Cat in the Hat's Learning Library;
Cat in the Hat's Learning Library;
Curious George Discovers;
Dr. Seuss; Cat in the Hat's Learning Library;
First Science (ABDO); Buddy Books (ABDO);
First Science (Bellwether); Blastoff! Readers;
First Science Experiments;
 
Upvote 0
OK, so I was able to work through a solution with some help, in part from this 2007 thread here:

The following VBA code is building a 1-dimensional array from a Named Range - l_Active_Series - that contains a list of values for a given Student; "John Doe", in my testing. I was able to test this on several sheets where a Series column was available.

Note: "AutoFilter's Criteria argument expects a 1-dimensional array." @John W

VBA Code:
Sub Filter_Active_Series()
   
    'Data Types & Declarations
    Dim ws As Worksheet: Set ws = Worksheets(wsName)    'Set Active Worksheet by Name
    Dim TempCriteria As Variant: TempCriteria = Range("l_Active_Series").Value   '2D Array from Named Range
    Dim SeriesCriteria() As String  '1D Array built in loop
    Dim SeriesIndex As Long: SeriesIndex = ws.ListObjects(1).ListColumns("Series").Index 'Set 'Series' column index number

    'Convert 2D to 1D Array
    ReDim SeriesCriteria(1 To UBound(TempCriteria))
        For i = 1 To UBound(TempCriteria)
            SeriesCriteria(i) = TempCriteria(i, 1)
        Next
   
    'Filter Active Series
    ws.ListObjects(1).Range.AutoFilter Field:=SeriesIndex, Criteria1:=SeriesCriteria, Operator:=xlFilterValues
   
End Sub

I used two Named Ranges to help me both insert a formula in "c_Active_Series" that would build the next Named Range "l_Active_Series" that contians the criteria needed - based on the Student name selected from a list; not shown here - to AutoFilter the book Series column.

Named Ranges... if curious:
Name
: c_Active_Series | Refers To: =Lists!$AJ$5

Name: l_Active_Series | Refers To: =Lists!$AJ$5#
 
Upvote 0
Solution

Forum statistics

Threads
1,223,884
Messages
6,175,175
Members
452,615
Latest member
bogeys2birdies

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