gpt4 book ai didi

vba - 坚持使用我的 vba 代码来过滤基于日期的查找数据

转载 作者:行者123 更新时间:2023-12-04 20:31:03 25 4
gpt4 key购买 nike

当我使用日期时这很好用,但是如果我尝试使用输入框输入我想要的日期来获取数据,它就不起作用......有什么建议吗?

sub datewise()
Dim lastrow As Long, erow As Long, i As Integer, lastcolumn As Long
Dim today, tomorrow As String
Dim k As Integer, j As Integer, s As Integer


today = InputBox(Prompt:="Please enter the date in d/m/yyyy format", Title:="Shift Date", Default:="7/6/2018")

'today = Date
'tomorrow = today + 1

lastrow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
j = 3
Do While j < 40
If today = Sheet1.Cells(2, j) Then
Sheet1.Cells(2, j).Copy
Sheet1.Paste Worksheets("Sheet2").Cells(2, 2)
Sheet1.Paste Worksheets("Sheet2").Cells(2, 4)
Sheet1.Paste Worksheets("Sheet2").Cells(2, 6)
k = j
Exit Do
End If
j = j + 1
Loop

'------1800 Shift------


i = 3
erow = 4
Do While i <= lastrow

If Sheet1.Cells(i, k).Value = 18 Then
Sheet1.Cells(i, 1).Copy
Sheet1.Paste Worksheets("Sheet2").Cells(erow, 1)

Sheet1.Cells(i, 2).Copy
Sheet1.Paste Worksheets("Sheet2").Cells(erow, 2)

erow = erow + 1
End If
i = i + 1
Loop



Application.CutCopyMode = False
Sheet2.Columns().AutoFit
Range("A1").Select
End Sub

我需要使用我的主表日期并根据我需要的日期对其进行过滤,我在想也许我需要使用自动过滤选项,但日期数据是连续的,所以过滤器也不能使用。如果我使用日期变量,此代码可以正常工作

最佳答案

我会强制使用特定格式:

Sub INeedADate()
Dim today As Date, d As String, arry
d = Application.InputBox(Prompt:="Please enter the date in d/m/yyyy format", Title:="Shift Date", Default:="7/6/2018", Type:=2)
arry = Split(d, "/")
today = DateSerial(arry(2), arry(1), arry(0))
End Sub

2,1,0 order 匹配您的输入格式。

关于vba - 坚持使用我的 vba 代码来过滤基于日期的查找数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50783800/

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