Filtering and a table by double clicking on the header.

Eggy66

New Member
Joined
May 5, 2024
Messages
3
Office Version
  1. 2021
Platform
  1. Windows
Say for example I have this table:
Untitled.png


How can I make it so that when I double click on the header "verb", it filters the table to show only the rows that contains X in that header's column. And when I double click on the header again the filters gets removed, and if I clicked on another header while the table is already filtered, it gets removed first before filtering it.

I used this VBA code:
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
   If Not Application.Intersect(Target, Range("A1:D1")) Is Nothing Then
      Cancel = True
      If Me.AutoFilterMode Then
         Me.AutoFilterMode = False
      Else
         Me.Range("A1:D1").AutoFilter Field:=Target.Column, Criteria1:="X"
      End If
   End If
End Sub

It works, however it doesn't remove the filter when I double click on the same header again, and it doesn't remove previous filters before filtering another column.

Any help would be much appreciated!
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Managed to solve it with the help of ChatGPT.
Here if anyone needs it:
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim tbl As ListObject
    Dim filterRange As Range
    
    Set tbl = Me.ListObjects("Table1") ' Change "Table1" to the name of your table
    
    ' Check if the double-clicked cell is in the header row
    If Not Intersect(Target, tbl.HeaderRowRange) Is Nothing Then
        ' Determine the column index of the double-clicked header
        Dim colIndex As Long
        colIndex = Target.Column - tbl.HeaderRowRange.Column + 1
        
        ' Get the filter range for the column
        Set filterRange = tbl.ListColumns(colIndex).DataBodyRange
        
        ' Toggle filter
        If filterRange.Parent.AutoFilter.FilterMode Then
            filterRange.Parent.AutoFilter.ShowAllData
        Else
            Me.Range("A1:D1").AutoFilter Field:=Target.Column, Criteria1:="?"
        End If
        
        Cancel = True ' Prevent default behavior of double-click
    End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,216,136
Messages
6,129,084
Members
449,485
Latest member
greggy

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