Hi all,
I'm trying to use a code that deletes all the worksheets except 3 when the year changes.
I have a work book that contains multiple sheets with dynamic names. Except the 3 main sheets the rest contain data which will not be useful after the current year end.
I'm pretty new with vba so it might silly what I'm doing. So, at the 1 sheet (Master SHeet) I'm using the A3 cell as year(today()) and A4 as the year of the latest data entered in any of my worksheets. So, I'm trying to achieve to delete all the sheets when the A3 is 2017. Next year, when A4 is 2018 and so on.
I'm using the following code:
In Master sheet:
and the following module at the workbook:
It might not be the correct way how I'm using but trying to use what I found online. The problem is that it might work once but not later on.
Pls, advice me if you have any idea
I'm trying to use a code that deletes all the worksheets except 3 when the year changes.
I have a work book that contains multiple sheets with dynamic names. Except the 3 main sheets the rest contain data which will not be useful after the current year end.
I'm pretty new with vba so it might silly what I'm doing. So, at the 1 sheet (Master SHeet) I'm using the A3 cell as year(today()) and A4 as the year of the latest data entered in any of my worksheets. So, I'm trying to achieve to delete all the sheets when the A3 is 2017. Next year, when A4 is 2018 and so on.
I'm using the following code:
In Master sheet:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$3" And Range("A3") > Range("A4") Then
Call DeleteSheets1
End If
End Sub
and the following module at the workbook:
Code:
Sub DeleteSheets1()
Dim xWs As Worksheet
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each xWs In Application.ActiveWorkbook.Worksheets
If xWs.Name <> "Master Sheet" And xWs.Name <> "Monthly Report" And xWs.Name <> "Default" Then
xWs.Delete
End If
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
It might not be the correct way how I'm using but trying to use what I found online. The problem is that it might work once but not later on.
Pls, advice me if you have any idea
Last edited: