Filter based on active cell vba code

anna99

New Member
Joined
Jan 8, 2021
Messages
16
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
  2. MacOS
  3. Web
Hi all, could you please help me. i'm trying to auto filter based on active cell, however, when i click onto empty cell, it will show an error. how do i fix this. my code curretnly as below and work well except for empty cell. i want to keep under worksheet so it can auto run

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Selection.AutoFilter Field:=ActiveCell.Column, Criteria1:=ActiveCell.Value

End Sub
 

Some videos you may like

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).

Osvaldo Palmeiro

Well-known Member
Joined
Feb 24, 2009
Messages
610
Office Version
  1. 365
Platform
  1. Windows
Hi, Anna.
Please, try this:
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 If Target.Value = "" Then Exit Sub
 Selection.AutoFilter Field:=ActiveCell.Column, Criteria1:=ActiveCell.Value
End Sub
 

anna99

New Member
Joined
Jan 8, 2021
Messages
16
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
  2. MacOS
  3. Web
Hi, Anna.
Please, try this:
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Value = "" Then Exit Sub
Selection.AutoFilter Field:=ActiveCell.Column, Criteria1:=ActiveCell.Value
End Sub
Thank you so much Osvaldo
 

anna99

New Member
Joined
Jan 8, 2021
Messages
16
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
  2. MacOS
  3. Web
Hi, Anna.
Please, try this:
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Value = "" Then Exit Sub
Selection.AutoFilter Field:=ActiveCell.Column, Criteria1:=ActiveCell.Value
End Sub
Hi Osvaldo, this works for empty cells, could you advise how i can remove the error/ debug if i choose the whole column, so if more than 1 cells chosen --> then exit sub, thanks for your help
 
Last edited by a moderator:

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
53,259
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Please do not put your response inside the quote, as it looks as though you have simply quoted a post without saying anything. I have change it for you this time.
 

Osvaldo Palmeiro

Well-known Member
Joined
Feb 24, 2009
Messages
610
Office Version
  1. 365
Platform
  1. Windows
Hi, Anna.
Try this instead.
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 If Target.Count > 1 Then Exit Sub
 If Target.Value = "" Then Exit Sub
 Selection.AutoFilter Field:=ActiveCell.Column, Criteria1:=ActiveCell.Value
End Sub
 

anna99

New Member
Joined
Jan 8, 2021
Messages
16
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
  2. MacOS
  3. Web
Hi, Anna.
Try this instead.
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Value = "" Then Exit Sub
Selection.AutoFilter Field:=ActiveCell.Column, Criteria1:=ActiveCell.Value
End Sub
Perfect, appreciate your help. just 1 last thing, could you please help to limit filter based on active cell values of Column A only instead of a whole worksheet.
 

Osvaldo Palmeiro

Well-known Member
Joined
Feb 24, 2009
Messages
610
Office Version
  1. 365
Platform
  1. Windows
Here you go.
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 If Target.Count > 1 Then Exit Sub
 If Target.Column > 1 Or Target.Value = "" Then Exit Sub
 Selection.AutoFilter Field:=ActiveCell.Column, Criteria1:=ActiveCell.Value
End Sub
 
Solution

Watch MrExcel Video

Forum statistics

Threads
1,123,231
Messages
5,600,427
Members
414,384
Latest member
joehalks

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