本例使用dictionary方式記錄不刪的工作表,然後對檔案中所有工作表檢查,只要不符合就刪掉,用VBA刪除工作表可以不顯示工作表就刪掉,對於檔案瘦身挺好用,這裡還用到inputbox對近期兩個工作表以輸入的方式保留。(2-3個月沒寫,的確有點生疏)
Sub 批次刪除工作表()
'2021/7/12
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.DisplayAlerts = False
Dim D_不刪
Set D_不刪 = CreateObject("scripting.dictionary")
Sh_當期 = InputBox("輸入當其表格名稱", , Format(Date, "yyyymmdd"))
Sh_上期 = InputBox("輸入當其表格名稱", , Format(Date - 7, "yyyymmdd"))
sh_不刪 = Array("月簡表", "簡表", "月比較A", "月明細", "周比較", Sh_當期, Sh_上期)
For i_sh = LBound(sh_不刪) To UBound(sh_不刪)
D_不刪(sh_不刪(i_sh)) = i_sh
Next
For i_sh = Sheets.Count To 1 Step -1
If D_不刪.exists(Sheets(i_sh).Name) = False Then Sheets(i_sh).Delete
Next
End Sub
Set D_不刪 = CreateObject("scripting.dictionary")
Sh_當期 = InputBox("輸入當其表格名稱", , Format(Date, "yyyymmdd"))
Sh_上期 = InputBox("輸入當其表格名稱", , Format(Date - 7, "yyyymmdd"))
sh_不刪 = Array("月簡表", "簡表", "月比較A", "月明細", "周比較", Sh_當期, Sh_上期)
For i_sh = LBound(sh_不刪) To UBound(sh_不刪)
D_不刪(sh_不刪(i_sh)) = i_sh
Next
For i_sh = Sheets.Count To 1 Step -1
If D_不刪.exists(Sheets(i_sh).Name) = False Then Sheets(i_sh).Delete
Next
End Sub
沒有留言:
張貼留言