在VBA中过滤日期超过特定日期的数据

时间:2020-09-19 08:18:16

标签: excel vba

我正在尝试过滤日期大于给定日期的数据,但是它没有选择完整的日期列表。

这是我的代码

Sub Vas()

    Startdate="13-10-2020"
    Selection.autofilter filed:=6,creteria1:=">=" & startdate

End sub

当我尝试使用equal(“ =”)进行过滤时,上述公式有效,但是当我使用大于时,公式失败。谁能帮忙!

3 个答案:

答案 0 :(得分:1)

日期是 DateTime ,而不是文本,因此:

Sub Vas()

    Dim StartDate as Date

    StartDate = #10/13/2020#
    Selection.Autofilter Field:=6,Criteria1:=">=" & StartDate

    ' or perhaps, as your default date format is dd-mm-yyyy:
    Selection.Autofilter Field:=6,Criteria1:=">=#" & Format(StartDate, "yyyy\/mm\/dd") & "#"

End sub

答案 1 :(得分:1)

一种更好的方法是将日期指定为数字(长),然后使用DateSerial函数,如下所示:

Sub Vas()
Dim StartDate As Long
    StartDate = DateSerial(2020, 10, 13)
    Selection.AutoFilter Field:=6, Criteria1:=">=" & StartDate
End Sub

答案 2 :(得分:0)

使用此

Sub Vas()

Startdate="13-10-2020"
Selection.autofilter filed:=6,creteria1:=">" & startdate

End sub