Trying to Avoid having to update VBA Code every year when the new year comes

jupasto

New Member
Joined
Aug 19, 2019
Messages
9
Hello I'm a VBA newb but I managed to patch together some code that works for what I need. When I open the workbook it automatically creates a duplicate worksheet of sheet "2020" and labels it "Scratch[date of cell B1]" (first deleting the previous scratch file). I was wondering if anyone could improve the code so that I don't have to keep changing it every time it's a new year. For instance when 2021 comes around I'll be working with worksheet "2021" so I'll have to manually update the VBA to change the reference from 2020 to 2021.

VBA Code:
Sub Workbook_Open()
    Dim sName
    sName = "Scratch" & Month(Range("B1")) _
            & "-" & Day(Range("B1")) _
            & "-" & Year(Range("B1"))
    Worksheets("2020").Copy after:=Sheets(Worksheets.Count)
    Dim ws As Worksheet
    Application.DisplayAlerts = False
    For Each ws In ThisWorkbook.Sheets
        If ws.Name Like "Scratch*" Then
            ws.Delete
        End If
    Next
    Application.DisplayAlerts = True
    ActiveSheet.Name = sName
    Worksheets("2020").Activate
 End Sub


Thanks in advance!
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
How about
VBA Code:
Sub Workbook_Open()
    Dim sName
    Dim Yr As String
    Yr = CStr(Year(Date))
    sName = "Scratch" & Month(Range("B1")) _
            & "-" & Day(Range("B1")) _
            & "-" & Year(Range("B1"))
    Worksheets(Yr).Copy after:=Sheets(Worksheets.Count)
    Dim ws As Worksheet
    Application.DisplayAlerts = False
    For Each ws In ThisWorkbook.Sheets
        If ws.Name Like "Scratch*" Then
            ws.Delete
        End If
    Next
    Application.DisplayAlerts = True
    ActiveSheet.Name = sName
    Worksheets(Yr).Activate
 End Sub
 
Upvote 0
Try this code, change this statement:
VBA Code:
Worksheets("2020").Copy after:=Sheets(Worksheets.Count)
to
Code:
Dim Yr As String
Dim nextyr As String

Yr = Year(Now())
nextyr = Year(Now()) + 1
Worksheets(Yr).Copy after:=Sheets(Worksheets.Count)
ActiveSheet.Name = nextyr
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,991
Messages
6,122,628
Members
449,095
Latest member
bsb1122

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