Filter based on active cell vba code

anna99

New Member
Joined
Jan 8, 2021
Messages
26
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
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
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
 
Upvote 0
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:
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
Solution

Forum statistics

Threads
1,214,400
Messages
6,119,288
Members
448,885
Latest member
LokiSonic

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