简体   繁体   English

在Excel VBA中的日期之间过滤

[英]Filter between dates in Excel VBA

I have a column in Excel VBA containing lots of dates of the format "dd.mm.yyyy hh:mm" I want to filter this column only between the Date (not by hours). 我在Excel VBA中有一列,其中包含许多格式为“ dd.mm.yyyy hh:mm”的日期,我只想在日期之间过滤此列(而不是按小时)。 Since it does not filter right using >= and <= (i guess because of the times) i wanted to try to filter the following way: 由于它不能使用> =和<=正确过滤(我想是因为时间原因),所以我想尝试过滤以下方式:

= StartDate and < EndDate+1 = StartDate和<EndDate + 1

The StartDate & EndDate is entered in a Textbox in the format mm/dd/yyyy by the user 用户在文本框中以mm / dd / yyyy格式输入StartDate和EndDate

and the way i tried to filter is: 我尝试过滤的方式是:

 EndDate_1 = DateAdd("d", 1, EndDate)
 EndDate_1 = Format(EndDate_1, "mm/dd/yyyy")

 Selection.AutoFilter Field:=1, Criteria1:=">=" & StartDate, Operator:=xlAnd, Criteria2:="<" & EndDate_1

But this does not work (all the Data is filtered out, no error message) 但这不起作用(所有数据都被过滤掉,没有错误消息)

If I just enter 如果我刚进入

  Selection.AutoFilter Field:=1, Criteria1:=">=" & StartDate, Operator:=xlAnd, Criteria2:="<=" & EndDate

without any formatting on StartDate and EndDate the filtering works (not correctly but at least it works :)) 没有在StartDate和EndDate上进行任何格式化的过滤工作(不正确,但至少可以工作:))

I tried so many thinks but I cant get it running! 我尝试了很多想法,但无法运行! Thank you for helping! 感谢您的帮助!

"<=" & EndDate is the same as "<=" & EndDate & " 00:00:00" "<=" & EndDate"<=" & EndDate & " 00:00:00"

So try this: 所以试试这个:

Selection.AutoFilter Field:=1, Criteria1:=">=" & StartDate, Operator:=xlAnd, Criteria2:="<=" & EndDate & " 23:59:59"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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