如何删除早于本月的工作表
How to delete sheets older than this month
我有一个包含各种 sheet 的工作簿(sheet 名称是采用这种格式的日期 DD.MM.YYYY)
我正在使用以下宏创建一个新的 sheet,删除给定范围内的任何内容,并在新的 sheet 上给出今天的日期:
ActiveSheet.Copy Before:=Sheets(1)
Range("B5:I" & Range("B4").End(xlDown).Row).Select
Selection.ClearContents
ActiveSheet.Name = Format(Date, "DD.MM.YYYY")
我还想创建一个新宏,以便从前几个月(除本月以外的所有内容)中删除 sheets。我已经在此线程上尝试了给定的解决方案 How to delete sheet older than a month? 但没有任何效果。
对 vba 不是很有经验,所以欢迎任何帮助。在 excel 2019 年工作。
请尝试下一种方式:
Sub deleteSheetsByMonth()
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
If IsDate(sh.Name) Then
If DateDiff("m", CDate(sh.Name), Date) > 0 Then sh.Delete
End If
Next sh
End Sub
有这样的隐藏表吗?
Option Explicit
Sub deleteSheetsByMonth()
Dim ws As Worksheet, a, dtWs As Date, dt1 As Date
Dim msg As String
dt1 = DateSerial(Year(Date), Month(Date), 1) ' 1st of month
For Each ws In ActiveWorkbook.Worksheets
If ws.Name Like "##.##.####" Then
a = Split(ws.Name, ".")
dtWs = DateSerial(a(2), a(1), a(0))
If dtWs < dt1 Then
msg = msg & vbLf & ws.Name
Application.DisplayAlerts = False
ws.Delete
Application.DisplayAlerts = False
End If
End If
Next
If msg <> "" Then
MsgBox "Sheets deleted:" & msg, vbInformation
Else
MsgBox "No Sheets deleted", vbInformation
End If
End Sub
我有一个包含各种 sheet 的工作簿(sheet 名称是采用这种格式的日期 DD.MM.YYYY)
我正在使用以下宏创建一个新的 sheet,删除给定范围内的任何内容,并在新的 sheet 上给出今天的日期:
ActiveSheet.Copy Before:=Sheets(1)
Range("B5:I" & Range("B4").End(xlDown).Row).Select
Selection.ClearContents
ActiveSheet.Name = Format(Date, "DD.MM.YYYY")
我还想创建一个新宏,以便从前几个月(除本月以外的所有内容)中删除 sheets。我已经在此线程上尝试了给定的解决方案 How to delete sheet older than a month? 但没有任何效果。
对 vba 不是很有经验,所以欢迎任何帮助。在 excel 2019 年工作。
请尝试下一种方式:
Sub deleteSheetsByMonth()
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Worksheets
If IsDate(sh.Name) Then
If DateDiff("m", CDate(sh.Name), Date) > 0 Then sh.Delete
End If
Next sh
End Sub
有这样的隐藏表吗?
Option Explicit
Sub deleteSheetsByMonth()
Dim ws As Worksheet, a, dtWs As Date, dt1 As Date
Dim msg As String
dt1 = DateSerial(Year(Date), Month(Date), 1) ' 1st of month
For Each ws In ActiveWorkbook.Worksheets
If ws.Name Like "##.##.####" Then
a = Split(ws.Name, ".")
dtWs = DateSerial(a(2), a(1), a(0))
If dtWs < dt1 Then
msg = msg & vbLf & ws.Name
Application.DisplayAlerts = False
ws.Delete
Application.DisplayAlerts = False
End If
End If
Next
If msg <> "" Then
MsgBox "Sheets deleted:" & msg, vbInformation
Else
MsgBox "No Sheets deleted", vbInformation
End If
End Sub