Checkbox to apply and remove a filter in a specific column

3link

Board Regular
Joined
Oct 15, 2010
Messages
141
I created a code for CheckBox57 that is intended to apply a filter to a specific column (field 15 within the filter range) when selected and remove that same filter when deselected. See below.

VBA Code:
Sub CheckBox57_Click()
Select Case CheckBox57
Case True
Dat = Format(Range("AY3").Value, "mm/dd/yyyy")
ActiveSheet.Range("A2:AT2").AutoFilter Field:=15, Criteria1:=">" & Dat
Case False
ActiveSheet.Range("A2:AT2").AutoFilter Field:=15
End Select
End Sub

The code does not work. Every time I click and unclick the checkbox, no filter is applied. Interestingly, if I invert the false and the true in the code, I get the opposite result. The filter is applied each time.

VBA Code:
Sub CheckBox57_Click()
Select Case CheckBox57
Case False
Dat = Format(Range("AY3").Value, "mm/dd/yyyy")
ActiveSheet.Range("A2:AT2").AutoFilter Field:=15, Criteria1:=">" & Dat
Case True
ActiveSheet.Range("A2:AT2").AutoFilter Field:=15
End Select
End Sub

I attempted changing it to an afterupdate event but that didn't work. I also attempted linking the event to the linked cell instead of the checkbox itself and got the same result.
 
Last edited:

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Try the following and let me know :

VBA Code:
Sub CheckBox57_Click()
    Dim Dat As String
    
    ' Check the value of the checkbox
    Select Case CheckBox57.Value
        Case True
            ' Format the date from AY3 as mm/dd/yyyy
            Dat = Format(Range("AY3").Value, "mm/dd/yyyy")
            
            ' Apply the filter on Field 15 with criteria greater than the date
            ActiveSheet.Range("A2:AT" & ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row).AutoFilter Field:=15, Criteria1:=">" & Dat
            
        Case False
            ' Remove the filter from Field 15
            ActiveSheet.Range("A2:AT" & ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row).AutoFilter Field:=15
    End Select
End Sub
 
Upvote 0
Try the following and let me know :

VBA Code:
Sub CheckBox57_Click()
    Dim Dat As String
   
    ' Check the value of the checkbox
    Select Case CheckBox57.Value
        Case True
            ' Format the date from AY3 as mm/dd/yyyy
            Dat = Format(Range("AY3").Value, "mm/dd/yyyy")
           
            ' Apply the filter on Field 15 with criteria greater than the date
            ActiveSheet.Range("A2:AT" & ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row).AutoFilter Field:=15, Criteria1:=">" & Dat
           
        Case False
            ' Remove the filter from Field 15
            ActiveSheet.Range("A2:AT" & ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row).AutoFilter Field:=15
    End Select
End Sub
I appreciate the reply. When I attempt to run that code I get a run-time error 424 "object required." It highlights the below text.

VBA Code:
Select Case CheckBox57.Value
 
Upvote 0
Try the following and let me know :

VBA Code:
Sub CheckBox57_Click()
    Dim Dat As String
   
    ' Check the value of the checkbox
    Select Case CheckBox57.Value
        Case True
            ' Format the date from AY3 as mm/dd/yyyy
            Dat = Format(Range("AY3").Value, "mm/dd/yyyy")
           
            ' Apply the filter on Field 15 with criteria greater than the date
            ActiveSheet.Range("A2:AT" & ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row).AutoFilter Field:=15, Criteria1:=">" & Dat
           
        Case False
            ' Remove the filter from Field 15
            ActiveSheet.Range("A2:AT" & ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row).AutoFilter Field:=15
    End Select
End Sub
I think I figured out the problem. If I use an activex checkbox instead, your code works. Thank you so much!
 
Upvote 0

Forum statistics

Threads
1,226,479
Messages
6,191,246
Members
453,649
Latest member
jtc19

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