在未经修改的XXX时间内删除工作表

时间:2017-06-05 12:54:58

标签: excel vba excel-vba

问题: 有没有办法可以删除尚未修改“X”天数的工作表?或者将非活动页面从我们的活动工作簿转移到存档簿中?

我正在开发一个项目,它接受所有活动工作表(从job#生成)并汇总所包含的数据。工作表名称都是可变的,并根据填充到主工作表中的数据生成。

作业完成后,可能永远不需要再次访问该工作表。

我很感激任何见解!

1 个答案:

答案 0 :(得分:1)

将其粘贴在ThisWorkbook对象中。它将在工作簿上任何工作表中完成的任何更改上更新跟踪器表(Sheet1此处)。然后在开始时,如果日期差异大于一周一次,它将(当取消注释时)删除工作表。如果你想做一些不同的事情,请改变这个

Option Explicit
Private Sub Workbook_Open()
    Dim rng As Range
    Dim DayDiff As Long
    Dim c

    DayDiff = 7
    ' Update this to the sheet that you want to keep your timestamp list
    With Sheet1
        Set rng = .Range(.Cells(1, 1), .Cells(.Cells(.Rows.Count, 1).End(xlUp).Row, 1))
        Application.DisplayAlerts = False
        For Each c In rng
            If c.Offset(0, 1) < Now() - DayDiff Then
                Debug.Print c
                'ThisWorkbook.Sheets(c.Value2).Delete
                'Range(c, c.Offset(0, 1)).Delete
            End If
        Next c
        Application.DisplayAlerts = True
    End With
End Sub
Private Sub Workbook_SheetChange(ByVal sh As Object, ByVal Target As Range)
    Dim rng As Range
    Dim c
    ' Update this to the sheet that you want to keep your timestamp list
    With Sheet1
        If Not sh.Name = .Name Then
            Set rng = .Range(.Cells(1, 1), .Cells(.Cells(.Rows.Count, 1).End(xlUp).Row, 1))
            Set c = rng.Find(sh.Name)
            If Not c Is Nothing Then
                c.Offset(0, 1) = Now()
            Else
                With .Cells(.Cells(.Rows.Count, 1).End(xlUp).Row + 1, 1)
                    .Value2 = sh.Name
                    .Offset(0, 1) = Now()
                End With
            End If
        End If
    End With
End Sub