VBA: How to check if a ListObject (Table) is being filtered on a specific field

Chris The Rock

Active Member
Joined
Feb 24, 2002
Messages
287
I'm editing a macro that came in a template downloaded from Microsoft.

I want it to do two new things when a cell is double-clicked, provided the double-clicked cell is in the column I want filtered:
1) Filter that table on the value in the double-clicked cell.
2) UN-filter that table on the value in the double-clicked cell, if the table's been filtered on that field.

I can't figure out what the syntax would be to check if the table has been filtered on a given field. All I can find is how to check if the table's been filtered on ANY field, and that won't work. I think once I know how to check that, I can design the rest of the changes in the macro.

I have the first part done. I can filter on a double clicked cell, but I want the user to be able to un-filter without having to know how to use the Autofilter controls.

Here's the code, mostly ripped off from the template:

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

    On Error Resume Next
    
    With Application
        .Cursor = xlNorthwestArrow
        BooleanCellDoubleClick Target, [Tasks[[Done]]], Cancel
        .Cursor = xlDefault
    End With
    
    If Intersect(Target.EntireRow, [Tasks[Owner]]) Is Nothing Then Exit Sub
    If Target.Cells.Count <> 1 Then Exit Sub
    
   'Check if double-clicked cell is the criteria for the filter on the Owner field
   'uh......how do I know if the table's been filtered on Field 6?
    
   'If there's no filter in place, filter based on the double-clicked cell
    With Sheet2
        .ListObjects(1).Range.AutoFilter Field:=6, Criteria1:=Intersect(Target.EntireRow, [Tasks[Owner]])
    End With
    Cancel = True
       
End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Figured it out: I looked through the object model until I found something I could reference.
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim lo As ListObject
Dim i As Integer

Set lo = Sheet2.ListObjects("Tasks")

    On Error Resume Next
    
    With Application
        .Cursor = xlNorthwestArrow
        BooleanCellDoubleClick Target, [Tasks[[Done]]], Cancel
        .Cursor = xlDefault
    End With
    
    If Intersect(Target.EntireRow, [Tasks[Owner]]) Is Nothing Then Exit Sub
    If Target.Cells.Count <> 1 Then Exit Sub
    
   'Check if double-clicked cell is the criteria for the filter on the Owner field (6)
    If lo.AutoFilter.Filters(6).On Then
    lo.Range.AutoFilter Field:=6
        Exit Sub
    End If
           
   'If there's no filter in place, filter based on the double-clicked cell
    lo.Range.AutoFilter Field:=6, Criteria1:=Intersect(Target.EntireRow, [Tasks[Owner]])

    Cancel = True
       
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,824
Messages
6,127,092
Members
449,358
Latest member
Snowinx

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