简体   繁体   中英

Filtering between two dates in excel vba

I need help to filter between two dates and it's giving me an error:

Named argument not found

my code

Dim StartDate As String
Dim EndDate As String
'
StartDate = Date
EndDate = Date + 365
'
ActiveSheet.AutoFilter Field:=7, Criteria1:=">=StartDate", Operator:=xlAnd, Criteria2:="<=EndDate"

Any help would be much appreciated!

if your dates are actual Date values (ie not String ones looking like dates), then go like this:

Dim StartDate As Date
Dim EndDate As Date
StartDate = Date
EndDate = Date + 365
ActiveSheet.Range("A1:AO1").AutoFilter Field:=7, Criteria1:=">=" & CDbl(StartDate), Operator:=xlAnd, Criteria2:="<=" & CDbl(EndDate)

I posted a sample file on my google drive.

https://drive.google.com/open?id=0B2w6b7-P-pX1R0dlTlFUSl9xZlE

I can describe the logic, but it's just easier for you to download it, play around with it, and see the mechanics of it.

Basic logic was the issue... mix up of the start and end date logic (should be EndDate = Date and StartDate = Date - 365).

Sorry for the troubles!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM