If Condition Before Filtering

Flani

New Member
Joined
Jul 11, 2012
Messages
17
So I am stuck with this code. What I want to happen is when rg is non blank, the macro will filter sheet imagine column b starting in row 12. down (number of rows varies everyday) and saves a screenshot (i have a macro code for this already). code below needs adjustments.


Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.ScreenUpdating = False
ActiveSheet.AutoFilterMode = False


Set rg = Sheets("MACRO").Range("F14")
Range("a12").AutoFilter
If rg.Value <> "" Then Range("A12").AutoFilter Field:=2, Criteria1:=rg.Value
End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
your code is failing because the range "A12" doesn't have a second column... you could try (although untested)

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.ScreenUpdating = False
ActiveSheet.AutoFilterMode = False




Set rg = Sheets("MACRO").Range("F14")
Range("a12").AutoFilter
If rg.Value <> "" Then Range("A12:B12").AutoFilter Field:=2, Criteria1:=rg.Value
'or If rg.Value <> "" Then Range("A12").AutoFilter Field:=1, Criteria1:=rg.Value
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,544
Messages
6,114,249
Members
448,556
Latest member
peterhess2002

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