Excel 比较两次,如果超过午夜,则标记为第二天
我有一个包含420000行登录条目的日志文件,我在D列有一个时间戳,在E列有一个登录名。现在我不知道日志文件是什么时候启动的,但我可以看到今天的登录在哪里,所以我希望通过工作表反向工作 现在我要问的问题是,我今天登录了,最后一个条目(向后工作)是: 那么它上面的一个是:Excel 比较两次,如果超过午夜,则标记为第二天,excel,vba,Excel,Vba,我有一个包含420000行登录条目的日志文件,我在D列有一个时间戳,在E列有一个登录名。现在我不知道日志文件是什么时候启动的,但我可以看到今天的登录在哪里,所以我希望通过工作表反向工作 现在我要问的问题是,我今天登录了,最后一个条目(向后工作)是: 那么它上面的一个是: 21:27:36 | Adam Jones 有没有一种方法可以在a列中运行,当您遇到这种情况时,开始输入前几天的日期 编辑: 这是我目前掌握的代码,我不知道这是否是最好的方法: Sub DateStamp() Dim
21:27:36 | Adam Jones
有没有一种方法可以在a列中运行,当您遇到这种情况时,开始输入前几天的日期
编辑:
这是我目前掌握的代码,我不知道这是否是最好的方法:
Sub DateStamp()
Dim WS1 As Worksheet
Dim lRow As Long
Set WS1 = ThisWorkbook.Sheets("Sheet1")
lRow = WS1.Cells(Rows.Count, 4).End(xlUp).Row
strDate = Date
Do Until lRow = 2
If WS1.Cells(lRow, 4).Value > WS1.Cells(lRow, 4).Offset(-1, 0).Value + TimeSerial(12, 0, 0) Then
strDate = strDate - 1
WS1.Cells(lRow, 4).Offset(0, -2).Value = strDate
Else
WS1.Cells(lRow, 4).Offset(0, -2).Value = strDate
End If
lRow = lRow - 1
Loop
End Sub
这段代码目前不太有效,因为任何接近午夜的时间都会将其标记为第二天
示例条目
提前感谢假设:
NotInteresting |DateGoesHere |NotInteresting |Time |Logon_Name
1 | |This |13:06:24 |Adam Jones
2 | |column |16:40:56 |Bill Bao
3 | |is |03:02:30 |Joe Bloggs
4 | |not |08:41:02 |Jhon Doe
5 | |interesting |19:30:36 |Iam Igor
6 | |but |04:06:52 |Pinco Pallino
7 | |I |13:49:59 |Romolo Remo
8 | |know |17:08:04 |Harry Potter
9 | |that |18:26:52 |Jane Doe
10 | |last |21:27:36 |Adam Jones
11 | |row |06:09:42 |Joe Bloggs
12 | |date |07:00:02 |Iam Igor
13 | |is today |12:40:08 |Pinco Pallino
预期结果是:
NotInteresting |DateGoesHere |NotInteresting |Time |Logon_Name
1 | 29/04/2020 |This |13:06:24 |Adam Jones
2 | 29/04/2020 |column |16:40:56 |Bill Bao
3 | 30/04/2020 |is |03:02:30 |Joe Bloggs
4 | 30/04/2020 |not |08:41:02 |Jhon Doe
5 | 30/04/2020 |interesting |19:30:36 |Iam Igor
6 | 01/05/2020 |but |04:06:52 |Pinco Pallino
7 | 01/05/2020 |I |13:49:59 |Romolo Remo
8 | 01/05/2020 |know |17:08:04 |Harry Potter
9 | 01/05/2020 |that |18:26:52 |Jane Doe
10 | 01/05/2020 |last |21:27:36 |Adam Jones
11 | 02/05/2020 |row |06:09:42 |Joe Bloggs
12 | 02/05/2020 |date |07:00:02 |Iam Igor
13 | 02/05/2020 |is today |12:40:08 |Pinco Pallino
If WS1.Cells(lRow, 4).Value > WS1.Cells(lRow, 4).Offset(-1, 0).Value + TimeSerial(12, 0, 0) Then
strDate = strDate - 1
End If
WS1.Cells(lRow, 4).Offset(0, -2).Value = strDate
此外,当你了解到如果一个单元格包含06:09:42,而上一个单元格包含21:27:36,那么你所说的是两个不同的日子时,你认为你介意什么?
这是因为上面的单元格比下面的单元格大,但是上面的单元格必须是比下面的单元格小的时间戳(日期+时间)
因此,如果
条件需要类似于:
If WS1.Cells(lRow, 4).Value < WS1.Cells(lRow, 4).Offset(-1, 0).Value Then
strDate = strDate - 1
End If
另外,在这些情况下,您可以避免使用偏移
WS1.Cells(lRow, 4).Offset(0, -2)
WS1.Cells(lRow, 4).Offset(-1, 0)
将它们替换为
WS1.Cells(lRow, 2)
WS1.Cells(lRow-1, 4)
到目前为止你试过什么?“前一行的时间比当前行的时间大吗?”@Chronocidal现在已经添加了代码,抱歉忘记了您的意思(任何接近午夜的时间都会将其标记为第二天)?看不到任何示例entry@Igor图像现在应该是可见的,当它到达前几天的时间时,从下到上工作(从06:09:42到21:27:36)我希望添加到B列的日期是前几天的日期。@lgor感谢您花时间解释我的错误所在以及如何改进代码。我已经运行了代码,它在一分钟内完成了所有430000行。改进的第一步:避免使用偏移量
Sub DateStamp()
Dim WS1 As Worksheet
Dim lRow As Long
Set WS1 = ThisWorkbook.Sheets("Sheet1")
lRow = WS1.Cells(Rows.Count, 4).End(xlUp).Row
strDate = Date
Do Until lRow = 2
WS1.Cells(lRow, 4).Offset(0, -2).Value = strDate
'If upper cell has a time bigger than the one in the current cell
'Than we are talking of two different days. Maybe is the day before.
If WS1.Cells(lRow, 4).Value < WS1.Cells(lRow, 4).Offset(-1, 0).Value Then
strDate = strDate - 1
End If
lRow = lRow - 1
Loop
End Sub
Sub DateStamp()
Dim WS1 As Worksheet
Dim lRow As Long
Set WS1 = ThisWorkbook.Sheets("Sheet1")
lRow = WS1.Cells(Rows.Count, 4).End(xlUp).Row
strDate = Date
Do Until lRow < 2 'It stops when lRow is strictly less than 2. Include lRow = 2
WS1.Cells(lRow, 4).Offset(0, -2).Value = strDate
'Doesn't run for lRow = 2
If lRow > 2 Then
If WS1.Cells(lRow, 4).Value < WS1.Cells(lRow, 4).Offset(-1, 0).Value Then
strDate = strDate - 1
End If
End If
lRow = lRow - 1
Loop
End Sub
WS1.Cells(lRow, 4).Offset(0, -2)
WS1.Cells(lRow, 4).Offset(-1, 0)
WS1.Cells(lRow, 2)
WS1.Cells(lRow-1, 4)