Simple VBA Autofilter

jtodd

Board Regular
Joined
Aug 4, 2014
Messages
194
Hi , I have the folowing simple code

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
With Worksheets("PutawayQ")
.Select
.Range("A1").AutoFilter Field:=1, Criteria1:=Target.Value
End With
End Sub

At present when I click any cell in the active sheet it filters by that value.
How can I make it will only work when a cell in Collumn A is sellected ,
I am guessing that it is something to do with the Range("A1").
I have tried various things ("A") , ("A1:A1000) ect but without any luck
Can anybody help
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

BarryL

Well-known Member
Joined
Jan 20, 2014
Messages
1,436
Office Version
  1. 2019
Platform
  1. Windows
  2. MacOS
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Column = 1 then Exit Sub
    Cancel = True
    With Worksheets("PutawayQ")
        .Select
        .Range("A1").AutoFilter Field:=1, Criteria1:=Target.Value
    End With
 End Sub
 
Upvote 0

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
40,681
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
There are different ways - for example:
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Column = 1 then
Cancel = True
With Worksheets("PutawayQ")
.Select
.Range("A1").AutoFilter Field:=1, Criteria1:=Target.Value
End With
End if
End Sub

or:
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If not intersect(Target, Range("A:A")) Is nothing then
Cancel = True
With Worksheets("PutawayQ")
.Select
.Range("A1").AutoFilter Field:=1, Criteria1:=Target.Value
End With
end if
End Sub
 
Upvote 0

Forum statistics

Threads
1,195,748
Messages
6,011,421
Members
441,614
Latest member
TiaGtz

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