vba 在excel vba中过滤两个日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40820757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 11:36:46  来源:igfitidea点击:

Filtering between two dates in excel vba

excelvbaexcel-vbafiltering

提问by PrivateResearch

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!

任何帮助将非常感激!

回答by user3598756

if your dates are actual Datevalues (i.e. not Stringones looking like dates), then go like this:

如果您的日期是实际Date值(即不是String看起来像日期的日期),那么像这样:

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)

回答by user3598756

I posted a sample file on my google drive.

我在我的谷歌驱动器上发布了一个示例文件。

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

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.

我可以描述其中的逻辑,但对您来说,下载它、使用它并查看它的机制会更容易。

回答by PrivateResearch

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

基本逻辑是问题......混合开始和结束日期逻辑(应该是 EndDate = Date 和 StartDate = Date - 365)。

Sorry for the troubles!

抱歉给您带来麻烦!