reset filter when double click same cell twice

hajiali

Well-known Member
Joined
Sep 8, 2018
Messages
624
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
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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?
 
Upvote 0
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.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,432
Members
448,961
Latest member
nzskater

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