Autofilter date by chosen year

Kemidan2014

Board Regular
Joined
Apr 4, 2022
Messages
229
Office Version
  1. 365
Platform
  1. Windows
Playing around with Macro recorder I got this code string because i need to use data by year and as time passes the year we want to look at will change
VBA Code:
    Rows("1:1").Select
    Selection.AutoFilter
        ActiveSheet.Range("$A$1:$Z$1046").AutoFilter Field:=15, Operator:= _
        xlFilterValues, Criteria2:=Array(0, "4/19/2022")

I know i need to clean this up but for now what could i do so this code to where i can for example highlight a cell for the user to type in the year and then the macro runs so that the sheet i am referencing will filter by that year?

i feel like theres a cell reference i need but not sure how to key it or where to put it with in the code
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
How about
VBA Code:
ActiveSheet.Range("$A$1:$Z$1046").AutoFilter Field:=15, Operator:= _
        xlFilterValues, Criteria2:=Array(0, datevalue("1/1/"&Range("AA1").Value))
Change the AA1 to reference the cell with the year.
 
Upvote 0
I will give your code a try

I tried to google the syntax for this type of filtering but i could not get a clear answer so is the ":=Array(0, "4/19/2022") is the date in quotes basically defining the date format?
 
Upvote 0
The 0 at the start of the array denotes to use the year only.
 
Upvote 0

Forum statistics

Threads
1,223,270
Messages
6,171,103
Members
452,379
Latest member
IainTru

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