VBA code to set autofilter based on start date and end date in seperate cells of the worksheet.

MrsFixIt

New Member
Joined
Apr 15, 2016
Messages
18
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a table with 23k rows of data and there is a column with dates from 2004 to current date. Each month I need to filter and copy the current month rows into a new worksheet. I want the macro to filter a start date in cell K1 and end date in cell K2.
I cannot the after/greater than K1 and before/less than K2 to work.

This is what I have so far.

Dim day1 As String
Dim day2 As String
day1 = Sheets("Load").Range("K1").Value 'start date
day2 = Sheets("Load").Range("K2").Value 'end date
'
ActiveSheet.Range("$B$2:$H$23751").AutoFilter Field:=5, Criteria1:= _
">" & day1, Operator:=xlAnd, Criteria2:="<" & day2
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Try this instead:

Code:
Dim day1 As Date
Dim day2 As Date
day1 = Sheets("Load").Range("K1").Value 'start date
day2 = Sheets("Load").Range("K2").Value 'end date
'
ActiveSheet.Range[COLOR=#333333]("$B$2:$H$23751").AutoFilter Field:=5[/COLOR], Criteria1:= _
">" & CDbl(day1), Operator:=xlAnd, Criteria2:="<" & CDbl(day2)
 
Upvote 0
Bless You! It works perfectly! Now all I need to do is copy the results into a new worksheet in the same file. Do you have anything like that in your cookbook?
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,692
Members
448,979
Latest member
DET4492

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