reset filter when double click same cell twice

hajiali

Active Member
Joined
Sep 8, 2018
Messages
381
Office Version
  1. 2016
Platform
  1. Windows
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim rngCol As Range
    Dim Nextnum As Long
  
    Set rngCol = Me.Columns("A:A")
    If Not Intersect(rngCol, Target) Is Nothing Then
        Cancel = True
        Select Case Target.Value
        Case Is <> "": Target.ClearContents
        Case Else
        Nextnum = WorksheetFunction.Max(rngCol) + 1
        Target.Value = Nextnum
        End Select
    End If
    If Not Intersect(Target, Range("C2")) Is Nothing Then
    Cancel = True
      ActiveSheet.Unprotect Password:="password"
      ActiveSheet.Range("$A$3:$Q$603").AutoFilter Field:=3, Criteria1:="OFF"
      ActiveSheet.Protect Password:="password", AllowFiltering:=True
End If
End Sub

using the above code im able to double click C2 and it Filters the Field with "OFF." I want to add the function to reset ("Select All") in the column filter when double click again.

Thanks
 

Some videos you may like

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

onlyadrafter

Well-known Member
Joined
Aug 19, 2003
Messages
5,703
Platform
  1. Windows
Hello,

does this work as expected?

VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim rngCol As Range
    Dim Nextnum As Long
  
    Set rngCol = Me.Columns("A:A")
    If Not Intersect(rngCol, Target) Is Nothing Then
        Cancel = True
        Select Case Target.Value
        Case Is <> "": Target.ClearContents
        Case Else
        Nextnum = WorksheetFunction.Max(rngCol) + 1
        Target.Value = Nextnum
        End Select
    End If
    If Not Intersect(Target, Range("C2")) Is Nothing Then
    Cancel = True
    ActiveSheet.Unprotect Password:="password"
    If ActiveSheet.AutoFilterMode = False Then
        ActiveSheet.Range("$A$3:$Q$603").AutoFilter FIELD:=3, Criteria1:="OFF"
      Else
        ActiveSheet.Range("$A$3:$Q$603").AutoFilter
      End If
      ActiveSheet.Protect Password:="password", AllowFiltering:=True
End If
End Sub
 

onlyadrafter

Well-known Member
Joined
Aug 19, 2003
Messages
5,703
Platform
  1. Windows
Hello,

if you want to keep filter drop downs try

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim rngCol As Range
    Dim Nextnum As Long
  
    Set rngCol = Me.Columns("A:A")
    If Not Intersect(rngCol, Target) Is Nothing Then
        Cancel = True
        Select Case Target.Value
        Case Is <> "": Target.ClearContents
        Case Else
        Nextnum = WorksheetFunction.Max(rngCol) + 1
        Target.Value = Nextnum
        End Select
    End If
    If Not Intersect(Target, Range("C2")) Is Nothing Then
        Cancel = True
        ActiveSheet.Unprotect Password:="password"
        With ActiveSheet
            If .AutoFilterMode And .FilterMode Then
                .ShowAllData
                .Cells.ClearOutline
            Else
                ActiveSheet.Range("$A$3:$Q$603").AutoFilter field:=3, Criteria1:="OFF"
            End If
      End With
    End If
    ActiveSheet.Protect Password:="password"
End Sub
 

hajiali

Active Member
Joined
Sep 8, 2018
Messages
381
Office Version
  1. 2016
Platform
  1. Windows

ADVERTISEMENT

So i added more columns by copying that code and changing
VBA Code:
If Not Intersect(Target, Range("C2")) Is Nothing Then
to "E2", "G2", "I2","K2", "M2", "O2"

and changing
field:=3 to its column numbers.

it will not let me filter multiple columns simultaneously when I double click say G2 it filters by OFF then if i click on C2 it resets all filters on all rows. I would like to have the option to do this simultaneously with other columns
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,358
Office Version
  1. 365
Platform
  1. Windows
How about
VBA Code:
   If Not Intersect(Target, Range("C2")) Is Nothing Then
      Cancel = True
      Me.Unprotect Password:="password"
      With ActiveSheet
         If .AutoFilterMode Then
            If .AutoFilter.Filters.Item(3).On Then
               .Range("A3:Q3").AutoFilter 3
            Else
               .Range("$A$3:$Q$603").AutoFilter field:=3, Criteria1:="OFF"
            End If
         Else
            .Range("$A$3:$Q$603").AutoFilter field:=3, Criteria1:="OFF"
         End If
      End With
   End If
   Me.Protect Password:="password"
End Sub
 

hajiali

Active Member
Joined
Sep 8, 2018
Messages
381
Office Version
  1. 2016
Platform
  1. Windows

ADVERTISEMENT

I will give this a try. To add same functionality to other columns I will just copy the code for each column and change the range (“c2”) to cell and change all the 3’s to the column number for each column is that correct?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,358
Office Version
  1. 365
Platform
  1. Windows
That's right, or you can use this
VBA Code:
   If Not Intersect(Target, Range("C2")) Is Nothing Then
      Cancel = True
      Me.Unprotect Password:="password"
      With ActiveSheet
         If .AutoFilterMode Then
            If .AutoFilter.Filters.Item(Target.Column).On Then
               .Range("A3:Q3").AutoFilter Target.Column
            Else
               .Range("$A$3:$Q$603").AutoFilter field:=Target.Column, Criteria1:="OFF"
            End If
         Else
            .Range("$A$3:$Q$603").AutoFilter field:=Target.Column, Criteria1:="OFF"
         End If
      End With
   End If
where you don't need to change the column refs.
 

hajiali

Active Member
Joined
Sep 8, 2018
Messages
381
Office Version
  1. 2016
Platform
  1. Windows
Just what I needed thanks Fluff
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,358
Office Version
  1. 365
Platform
  1. Windows
Glad we could help & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,205
Messages
5,623,364
Members
415,969
Latest member
Rey99

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
Top