gpt4 book ai didi

excel - 使用 InputBox 按确切日期过滤工作表的 VBA 代码

转载 作者:行者123 更新时间:2023-12-03 03:16:43 29 4
gpt4 key购买 nike

我们正在尝试让这段代码按确切日期过滤工作表,但仍然无法弄清楚,任何想法出了什么问题吗?日期在工作表中的格式为 mm/dd/yyyy

Sub FilterDate()
Dim ap, dt As Date, dt1 As Date, lr&: lr = ActiveSheet.UsedRange.SpecialCells(11).Row

With ActiveSheet.[a1].CurrentRegion.Resize(lr, 10)
ap = Application.InputBox("get date")
dt = CDate(ap)
.AutoFilter 10, "=" & dt
End With
End Sub

File sample

感谢您指出声明问题,我在 Excel 工具中进行了更改,但代码似乎还有另一个问题,任何日期输入都只留下顶行...

最佳答案

你缺少声明变量!!使用 Option Explicit 将使您的生活变得更加轻松,因为您无法在没有声明变量的情况下运行代码。

在 VBA 编辑器中,转到:“工具”->“选项”->“Require Variable Declaration”。

Sub FilterDate()
Dim ap As Date, dt As Date, dt1 As Date
Dim lr As Long
lr = ActiveSheet.UsedRange.SpecialCells(11).Row
With ActiveSheet.[a1].CurrentRegion.Resize(lr, 10)
ap = Application.InputBox("get date")
dt = CDate(ap)
'dt = CDate("2018-10-26")
.AutoFilter 10, Criteria1:=">=" & dt, _
Operator:=xlAnd, Criteria2:="<" & dt + 1
End With
End Sub

enter image description here

<小时/>

编辑:

是的,既然你有约会,你需要 filter in an interval ,而不是一个“=”条件(您的日期在这两个条件之间的两个条件),不要忘记创建一个标题,因为标题值始终存在(代码已更新)。以下示例的日期为 2018-10-26。

enter image description here

关于excel - 使用 InputBox 按确切日期过滤工作表的 VBA 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53142289/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com