删除超过3天的行

时间:2015-01-09 02:41:28

标签: excel vba date excel-vba

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

正如你所看到的那样,所有这些细胞显然都是从一个日期开始的,但是随之而来的是包含它的注释,这使得它变得困难。基本上我正在尝试编写一些VBA代码,允许我删除从当前日期开始超过3天的行。

1 个答案:

答案 0 :(得分:0)

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