Filtering data when Toogle button is on

Guinaba

Board Regular
Joined
Sep 19, 2018
Messages
217
Office Version
  1. 2016
Platform
  1. Windows
Hello experts,

Have a toogle button in my sheet filtering a specific column (code below), the filtering criteria is cell (17,5).value. What I'd like to do is when the filter is still on if the cell (17,5).value is changed the filter automatically changes as well.

VBA Code:
Private Sub ToggleButton1_Click()
   Dim LRow As Integer
   
  With ActiveSheet
  
    LRow = .Cells(.Rows.Count, "E").End(xlUp).Row
    
    If ToggleButton1.Value = True Then
      ToggleButton1.Caption = "Filter Item On"
      ToggleButton1.BackColor = vbGreen
      .Range("$A$21:$AF$" & LRow).AutoFilter Field:=4, Criteria1:=.Cells(17, 5).Value
    Else
      ToggleButton1.Caption = "Filter Item Off"
      ToggleButton1.BackColor = vbWhite
      .Range("$A$21:$AF$" & LRow).AutoFilter Field:=4
    End If
    
  End With

End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
How about this:

In the Worksheet Module that has the ToggleButton1 on it, paste these two codes (it can be done with one large Copy/Paste)

VBA Code:
Private Sub ToggleButton1_Click()

   Call FilterOn
   
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Cells.Count > 1 Then Exit Sub
    If Target = Range("E17") Then Call FilterOn
    
End Sub

Then in another module of your choosing- "Module1" for example. Paste this code.

VBA Code:
Sub FilterOn()

    Dim ws As Worksheet: Set ws = ActiveSheet
    Dim LRow As Integer
     
    With ws

        LRow = .Cells(.Rows.Count, "E").End(xlUp).Row
      
        If ActiveSheet.ToggleButton1.Value = True Then
            ActiveSheet.ToggleButton1.Caption = "Filter Item On"
            ActiveSheet.ToggleButton1.BackColor = vbGreen
            .Range("$A$21:$AF$" & LRow).AutoFilter Field:=4, Criteria1:=.Cells(17, 5).Value
        Else
            ActiveSheet.ToggleButton1.Caption = "Filter Item Off"
            ActiveSheet.ToggleButton1.BackColor = vbWhite
            .Range("$A$21:$AF$" & LRow).AutoFilter Field:=4
        End If
      
    End With

End Sub
 
Upvote 0
Solution
How about this:

In the Worksheet Module that has the ToggleButton1 on it, paste these two codes (it can be done with one large Copy/Paste)

VBA Code:
Private Sub ToggleButton1_Click()

   Call FilterOn
  
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Cells.Count > 1 Then Exit Sub
    If Target = Range("E17") Then Call FilterOn
   
End Sub

Then in another module of your choosing- "Module1" for example. Paste this code.

VBA Code:
Sub FilterOn()

    Dim ws As Worksheet: Set ws = ActiveSheet
    Dim LRow As Integer
    
    With ws

        LRow = .Cells(.Rows.Count, "E").End(xlUp).Row
     
        If ActiveSheet.ToggleButton1.Value = True Then
            ActiveSheet.ToggleButton1.Caption = "Filter Item On"
            ActiveSheet.ToggleButton1.BackColor = vbGreen
            .Range("$A$21:$AF$" & LRow).AutoFilter Field:=4, Criteria1:=.Cells(17, 5).Value
        Else
            ActiveSheet.ToggleButton1.Caption = "Filter Item Off"
            ActiveSheet.ToggleButton1.BackColor = vbWhite
            .Range("$A$21:$AF$" & LRow).AutoFilter Field:=4
        End If
     
    End With

End Sub
Thanks @igold! It works perfectly!
 
Upvote 0
You're welcome. I was happy to help. Thanks for the feedback!
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,796
Members
449,095
Latest member
m_smith_solihull

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