PrivateResearch
PrivateResearch

Reputation: 11

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!

Upvotes: 1

Views: 27256

Answers (3)

PrivateResearch
PrivateResearch

Reputation: 11

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!

Upvotes: 0

user7075507
user7075507

Reputation:

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.

Upvotes: 0

user3598756
user3598756

Reputation: 29421

if your dates are actual Date values (i.e. 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)

Upvotes: 4

Related Questions