vba 过滤今天之前的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24334726/
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 03:32:45  来源:igfitidea点击:

Filter dates before today

excelvba

提问by Modudes

I found and edited code. It is supposed to filter column O and only show dates less than or before today. Cell AC2 shows the date today. I can't even run the code.

我找到并编辑了代码。它应该过滤列 O 并且只显示小于或早于今天的日期。单元格 AC2 显示今天的日期。我什至无法运行代码。

With Sheets(1)
MyVal = Application.WorksheetFunction.WorkDay(Sheets(1).Range("AC2").Value)
Sheets(1).Range("A1:AA1").AutoFilter Field:=15, Criteria1:="<=" & MyVal

回答by Liza

If I understand correctly, the below should do the trick. It will filter to show only dates before the current date, whenever the code is run, according to your computer's system time:

如果我理解正确,下面应该可以解决问题。无论何时运行代码,它都会根据您计算机的系统时间进行过滤以仅显示当前日期之前的日期:

Sub DateFilter()
Sheets(1).Range("A1:AA1").AutoFilter Field:=15, Criteria1:="<" & Date
End Sub

If you want today to be included, change "<" to "<=". "Date" is the VBA way to refer to the current date. It uses your computer's system time.

如果要包括今天,请将“<”更改为“<=”。“日期”是引用当前日期的 VBA 方式。它使用您计算机的系统时间。

The WorkDay function is used to find a date a certain number of workdays after another date. You use it like this:

WorkDay 函数用于在另一个日期之后的某个工作日内查找某个日期。你像这样使用它:

WorkDay(Starting Date, Number of Workdays to Count)

WorkDay(开始日期,要计算的工作日数)

So, if you wanted to filter Column O based on, say, anything before the date five workdays after a date given in cell AC2, you could use:

因此,如果您想根据单元格 AC2 中给出的日期后五个工作日之前的任何日期过滤列 O,您可以使用:

Sub Datefilter()
Dim MyVal As Date
MyVal = Application.WorksheetFunction.WorkDay(Range("AC2").Value, 5)
Sheets(1).Range("A1:AA1").AutoFilter Field:=15, Criteria1:="<" & MyVal
End Sub

回答by Chitra Sena Bhuyan

Sub Test()

Dim MyVal As Date

MyVal = Range("AC2").Value

ActiveSheet.Range("A:O").AutoFilter 15, ">=" & CDbl(MyVal)

End Sub

回答by Damon Lim

Can try filter date for example after which date. Field, 7th column.

可以尝试过滤日期,例如在哪个日期之后。字段,第 7 列。

ActiveSheet.Range("A:G").AutoFilter Field:=7, Criteria1:=">" & CLng(CDate("12/31/2015")), Operator:=xlAnd

ActiveSheet.Range("A:G").AutoFilter Field:=7, Criteria1:=">" & CLng(CDate("12/31/2015")), Operator:=xlAnd

回答by Gray wolf

LL = Cells(1000000, "G").End(xlUp).Row 'last cell in column g
ActiveSheet.Range(Cells(1, 1), Cells(LL, 7)).AutoFilter Field:=7, Criteria1:=">=" & CDbl(Date - 3)