Delete Worksheets After Year Changes

GeoKoro13

New Member
Joined
Nov 24, 2016
Messages
27
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:
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:

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
I would be tempted to back up the file securely, then remove the unused sheets from the current file, people always want to know what happened eventualy
 
Upvote 0
I would be tempted to back up the file securely, then remove the unused sheets from the current file, people always want to know what happened eventualy

Thanks mate. Yeah, I have two files saved every time I use the spreadsheet.
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,741
Members
449,050
Latest member
excelknuckles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top