Filtering between two dates in excel vba

不打扰是莪最后的温柔 提交于 2020-01-17 04:38:26

问题


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!


回答1:


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)



回答2:


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.




回答3:


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!



来源:https://stackoverflow.com/questions/40820757/filtering-between-two-dates-in-excel-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!