Excel VBA搜索日期范围并删除旧条目

时间:2017-04-03 16:47:57

标签: excel vba excel-vba

如果完成日期过去90天,我需要一个宏来搜索整列完成日期并删除相应的数据行。这是我到目前为止所拥有的。

Dim RowDate
Dim CurrentDate
Dim Interval
Dim CurrentAddress
Dim ValueCellRange As Range
Dim ValueCell As Range

'Interval set to an appropriate number of days
Interval = 90
CurrentDate = Now()

'Identify starting row for sweep
Set ValueCellRange = Range("G2:G100")

'Set loop to execute until empty cell is reached

For Each ValueCell In ValueCellRange
    If CurrentDate - ValueCell.Value >= Interval Then
        ValueCell.EntireRow.ClearContents
    End If
Next ValueCell

'Clear variable value for next initialization
Set ValueCell = Nothing

1 个答案:

答案 0 :(得分:1)

如果您只想跳过空白,请在循环中添加另一个if语句

编辑通过工作表添加循环

Dim ws as Worksheet
For Each ws in Worksheets
    Set ValueCellRange = ws.Range("G2:G100")
    For Each ValueCell In ValueCellRange
        If ValueCell.Value <> "" Then
            If CurrentDate - ValueCell.Value >= Interval Then
                ValueCell.EntireRow.ClearContents
            End If
        End If
    Next ValueCell
Next ws