基于输入框日期的过滤器不起作用


0

我想输入开始日期和结束日期,并让VBA过滤所选日期之间的Excel表格中的数据条目。但是,此代码返回零过滤条目。如果我手动使用Excel中的过滤器,我发现它已正确配置(日期放在过滤条件框中)。如果我然后单击确定按钮它会过滤。知道我犯了什么错误吗?

附:假设输入框值是正确格式化的日期

Option Explicit

Sub ExpCsmLg()

' ExpCsmLg Makro
' this makro filters all data entries between two selected dates

Dim sdt As Date
Dim edt As Date

'sdt = InputBox("Choose Start date.")

'edt = InputBox("Choose End date.")


    ActiveSheet.Range("$A$5:$Q$7992").AutoFilter Field:=1, Criteria1:=">=" & sdt, Operator:=xlAnd, Criteria2:="<=" & edt


End Sub

Answers:


0

这似乎对我有用:

Option Explicit

Sub ExpCsmLg()

' ExpCsmLg Makro
' this makro filters all data entries between two selected dates

Dim sdt As Date
Dim edt As Date

sdt = CDate(Application.InputBox("Choose Start date.", Type:=2))

edt = CDate(Application.InputBox("Choose End date.", Type:=2))


    ActiveSheet.Range("$A$5:$Q$7992").AutoFilter Field:=1, Criteria1:=">=" & sdt, Operator:=xlAnd, Criteria2:="<=" & edt


End Sub

之前:

enter image description here

之后:

enter image description here

注意:

列中的项目 一个 是实际日期而不是文本值。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.