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

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
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
Instead of Selection.Autofilter.... I think you should be applying this to a specific range.
 
Upvote 0
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,214,791
Messages
6,121,611
Members
449,038
Latest member
apwr

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