Toggle filter on single column in VBA

tomleitch

Board Regular
Joined
Jan 3, 2012
Messages
189
Hi,

I have a button on my spreadsheet that I want to make toggle a filter in column AP...


So when clicked once I want it to filter everything but blanks in that column

When it is clicked again I want it to clear that filter for just that column.


I'm sure it's pretty straightforward, but I can't seem to work it out!


Any help much appreciated.

Thanks
T
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Give this a try:
- applies to active-x command button placed on the worksheet
- amend to match your range and command button

goes in sheet module
Code:
Private Sub CommandButton1_Click()
    Const col = "AP"
    Dim r As Range:         Set r = Me.[COLOR=#ff0000]Range("A:AZ")[/COLOR]
    Dim cb As Object:       Set cb = [COLOR=#ff0000]CommandButton1[/COLOR]
    Dim fld As Long:        fld = Columns(col).Column

'toggle
    On Error Resume Next: Me.ShowAllData
    r.AutoFilter
    Select Case cb.Caption = "Blanks"
        Case True
            cb.Caption = "All":         r.AutoFilter Field:=fld
        Case False
            cb.Caption = "Blanks":      r.AutoFilter Field:=fld, Criteria1:="<>", Operator:=xlAnd
    End Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,560
Messages
6,114,309
Members
448,564
Latest member
ED38

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