Excel VBA:删除旧logging
我试图删除180天或更长的logging。 date在F列。当我运行这个,没有任何反应。 我在想,这与Date()函数有关。
Sub ClearOldData() Application.ScreenUpdating = False Sheets("Data").Select Dim LastRow As Long With ActiveSheet LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row End With For i = 2 To LastRow Dim recdate As Date recdate = Cells(i, "F").Value If DateDiff(d, Date, recdate) > 179 Then ws.Rows(i).Delete End If Next i Application.ScreenUpdating = True End Sub
在VBAdate/时间是从1开始的数值,相当于#1/1/1900#。
date/时间值
- 1天= 1
- 1小时= 1/24
- 1分钟= 1/24/60
- 1秒= 1/24/60/60
一如往常从最后一行/项目删除到第一个。 我的答案比较单元格删除行,值为true,但不删除行演示原因。
DateDiff("d", Date, recdate) > 179
和Int(Date - recdate) > 179
是等效的。
Sub ClearOldData() Application.ScreenUpdating = False Dimi As Long With Sheets("Data") For i = 2 To LastRow Step -1 If Int(Date - Cells(i, "F").Value) > 179 Then .Rows(i).Delete End If Next i End With Application.ScreenUpdating = True End Sub