vba 月份期间的 Excel 自动筛选日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18646952/
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
Excel autofilter dates in month period
提问by Ben
The following code filters a range of dates by the dates in August
以下代码按八月的日期过滤日期范围
ActiveSheet.Range("$A:$CG82").AutoFilter field:=18, Criteria1:= _
xlFilterAllDatesInPeriodAugust, Operator:=xlFilterDynamic
How do I make this code dynamic so that I can filter the range by any month (e.g. month(today()))?
如何使此代码动态化,以便我可以按任何月份(例如月份(今天()))过滤范围?
采纳答案by Ben
Ended up doing...
结果做...
Function GetDatesInPeriodConst(month As Long)
GetDatesInPeriodConst = 20 + month
'(January is number 21)
End Function
and
和
ActiveSheet.Range("$A:$CG82").AutoFilter field:=18, Criteria1:= _
GetDatesInPeriodConst(month(Date)), Operator:=xlFilterDynamic
回答by KekuSemau
Try Criteria2
, this is a bit cryptic though. Pass Array(1, datestring)
, where 1 will filter the entire month of that date, and the datestring must be in m/d/y format.
(0 = entire year, 2 = day, and some more, as discussed somewhere here).
试试看Criteria2
,虽然这有点神秘。Pass Array(1, datestring)
,其中 1 将过滤该日期的整个月份,并且日期字符串必须采用 m/d/y 格式。
(0 = 全年,2 = 天,还有更多,如此处某处所讨论)。
Dim s As String
s = Format(Now, "MM""/""dd""/""yyyy")
ActiveSheet.Range("$A:$CG82").AutoFilter _
Field:=18, Operator:=xlFilterValues, _
Criteria2:=Array(1, s)