VB to filter

ccoury09

Board Regular
Joined
Jun 5, 2008
Messages
58
I am using the below code in a worksheet, so that when a user double clicks on a cell, the list should be filtered to display those rows with that value. When I do it, the box comes up to tell me the filter has been applied, but the sheet doesn't actually filter.

I know I am missing a line of code, but can't figure out for the life of me. Have been playing for hours, with no success.... Any suggestions would be GREAT!


Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
On Error Resume Next
Err.Clear
Selection.AutoFilter Field:=2, Criteria1:=Target.Value
MsgBox Target.Value & " is Filtered"
End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,358
Office Version
  1. 365
Platform
  1. Windows
Well my first suggestion would be that you aren't actually missing a line of code.:)

But you do have one that could be obscuring any errors.

ie
Code:
On Error Resume Next
 
Upvote 0

njimack

Well-known Member
Joined
Jun 17, 2005
Messages
7,772
Instead of Selection.Autofilter.... I think you should be applying this to a specific range.
 
Upvote 0

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Try:

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    With Range("A1").CurrentRegion
        If Not Application.Intersect(Target, .Cells) Is Nothing Then
            .AutoFilter
            .AutoFilter Field:=Target.Column, Criteria1:=Target.Value
            MsgBox Target.Value & " is Filtered"
            Cancel = True
        End If
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,191,691
Messages
5,988,113
Members
440,126
Latest member
duque00

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