删除日期早于 3 天的行

Deleting rows dated older than 3 days old

我对此很陌生,如果可以的话,请帮助我或给我一些提示。基本上我有一列 (N),其中包含单元格中带有日期和注释的行。例如,"12/20 Notes:-Left start site", "09/23 Notes:- Needs more information".....

如您所见,所有这些单元格显然都以日期开头,但其中包含注释,这使得它变得困难。基本上我正在尝试编写一些 VBA 代码,这些代码将允许我删除距离当前日期超过 3 天的行。

Sub test()

Dim rgNote As Range, clNote As Range
Dim noteDate As Variant, splitDate As Variant, splitNote As Variant
Dim expiredData

' 决定票据被认为过期的时间

expiredData = DateSerial(Year(Now), Month(Now), Day(Now)) - 3

' 找到要检查的单元格

Set rgNote = Intersect(Range("N:N"), Range("N:N").Parent.UsedRange)
If Not rgNote Is Nothing Then
    For Each clNote In rgNote.Cells

' 确认这确实是一张便条。这实际上取决于电子表格的结构

        splitNote = Split(clNote.Formula)
        If UBound(splitNote) > LBound(splitNote) Then
            splitDate = Split(splitNote(LBound(splitNote)), "/")
            If UBound(splitDate) = LBound(splitDate) + 1 Then

' 提取日期,在这种情况下,我们期望它们是 mm/dd 格式,并假设它们都是最近的

                noteDate = DateSerial(Year(Now), splitDate(LBound(splitDate)), splitDate(LBound(splitDate) + 1))
                If noteDate > Date Then
                    noteDate = DateSerial(Year(noteDate) - 1, Month(noteDate), Day(noteDate))
                End If

' 如果过期,则删除该行

                If noteDate <= expiredData Then
                    clNote.EntireRow.Delete
                End If
            End If
        End If
    Next clNote
End If

End Sub