Advanced filter using a range of date

monmon

Board Regular
Joined
Apr 1, 2013
Messages
84
Hi all, I need help here.

I've been trying this for hours and can't find a solution to this.

What I have is a set of data and I'm filtering to find out all data between a range of dates.

the Criteria would be anything that is >=today() and <=today-8.

this is what i have done and it does not work at all. please help

Sheets(2).Select

Dim dDate As Date
Dim lDate As Date

Range("N2").Value = "=today()"
Range("o2").Value = "=today()-8"

dDate = Range("N2")
lDate = Range("O2")

Range("A1").AutoFilter
Range("A1").AutoFilter Field:=10, Criteria1:=">=" & lDate, Operator:=xlAnd, Criteria2:="<=" & dDate
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Change the Dim dDate As Date to
Dim dDate As Long

Same for the other one.
 
Upvote 0
Hi all, I need help here.

I've been trying this for hours and can't find a solution to this.

What I have is a set of data and I'm filtering to find out all data between a range of dates.

the Criteria would be anything that is >=today() and <=today-8.

this is what i have done and it does not work at all. please help

Sheets(2).Select

Dim dDate As Date
Dim lDate As Date

Range("N2").Value = "=today()"
Range("o2").Value = "=today()-8"

dDate = Range("N2")
lDate = Range("O2")

Range("A1").AutoFilter
Range("A1").AutoFilter Field:=10, Criteria1:=">=" & lDate, Operator:=xlAnd, Criteria2:="<=" & dDate

Try changing your Date variables as Long & then pass them through DateSerial & see if that helps.

Something like this:

Code:
Sub FilterDates()
    Dim dDate As Long
    Dim lDate As Long
    With Sheets(2)
        .Range("N2").Formula = "=today()"
        .Range("O2").Formula = "=today() - 8"
        dDate = DateSerial(Year(.Range("N2").Value), Month(.Range("N2").Value), Day(.Range("N2").Value))
        lDate = DateSerial(Year(.Range("O2").Value), Month(.Range("O2").Value), Day(.Range("O2").Value))
        .Range("A1").AutoFilter
        .Range("A1").CurrentRegion.AutoFilter Field:=.Range("J1").Column, _
                                              Criteria1:=">=" & lDate _
                                            , Operator:=xlAnd, _
                                              Criteria2:="<=" & dDate
    End With
End Sub

Dave
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,809
Members
449,048
Latest member
greyangel23

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