VBA Double Click Column Heading to Filter NonBlank Cells

macpen

New Member
Joined
Mar 27, 2017
Messages
4
Hello,

I have a spreadsheet with different skills listed as column headings in the table I named "Train". I would like to be able to double click on any of the column headings and have my table filter on all non-blank cells. The rows have trainings listed so the table is full of x's that designate tranings that are relevant to the given skill.

I really appreciate your help!
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hi,

Try the below, you'll need to add it in the worksheet (not a module).

Just update the ranges A1:G1 to where your headers are. I'm assuming your data starts in column A also.

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Not Application.Intersect(Target, Range("A1:G1")) Is Nothing Then
Dim ws As Worksheet
Dim fieldnum As Long
Set ws = ActiveSheet
    fieldnum = ActiveCell.Column
    ws.Range("A1:G1").AutoFilter Field:=fieldnum, Criteria1:="x"
    End If
End Sub
 
Last edited:
Upvote 0
Thank you! This works great! What would I add at the end if after it's filtered on the x's I double click again to unfilter it back to how it was initially?
 
Upvote 0
Here's a bit of a workaround for that. You'll just need to double click somewhere outside of where you're declaring the headers to be.

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Not Application.Intersect(Target, Range("A1:G1")) Is Nothing Then
Dim ws As Worksheet
Dim fieldnum As Long
Set ws = ActiveSheet
    fieldnum = ActiveCell.Column
    ws.Range("A1:G1").AutoFilter Field:=fieldnum, Criteria1:="x"
    Else
    ActiveSheet.AutoFilterMode = False
    End If
End Sub
 
Upvote 0
Another option
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
   If Not Application.Intersect(Target, Range("A1:G1")) Is Nothing Then
      Cancel = True
      If Me.AutoFilterMode Then
         Me.AutoFilterMode = False
      Else
         Me.Range("A1:G1").AutoFilter Field:=Target.Column, Criteria1:="x"
      End If
   End If
End Sub
First time you doubleclick a header it will filter that column, the next time it will remove the filter
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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