VBA: How to copy worksheet number to new worksheet and increment worksheet number by 1

VBA Challenged

New Member
Joined
Jan 14, 2022
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
In the same workbook, I want to copy an active worksheet to a new worksheet and name the new worksheet with the previous named worksheet and increment that name (number) by 1. The worksheet name is a four digit number (it is also a year). The first worksheet is named 2021. When I run the macro, it copies the contents of the 2021 worksheet into a new worksheet and applies the name of 2022. If I run the macro again, then the new worksheet is titled 2022 (2), instead of 2023. You will notice that my code is treating the worksheet name as a date. I did this because I didn't know how to code it with numbers. Either way is acceptable to me. Need help such that the worksheet names are 2022, 2023, 2024, etc. Here is the VBA code that I am using:

VBA Code:
Sub Macro5()
'
    On Error Resume Next
    ActiveSheet.Select
    ActiveSheet.Copy After:=ActiveSheet
    ActiveSheet.Name = Format(DateAdd("y", 1, Date), "YYYY")
    If Err.Number <> 0 Then ActiveSheet.Activate
        
 End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
this should do it:
VBA Code:
Sub Macro5()
'
    Dim tmpr As Long
'    On Error Resume Next
    tempr = ActiveSheet.Name
    If IsNumeric(tempr) Then
    tempr = tempr + 1
    ActiveSheet.Copy After:=ActiveSheet
    ActiveSheet.Name = tempr
    End If
 End Sub
 
Upvote 0
Solution
this should do it:
VBA Code:
Sub Macro5()
'
    Dim tmpr As Long
'    On Error Resume Next
    tempr = ActiveSheet.Name
    If IsNumeric(tempr) Then
    tempr = tempr + 1
    ActiveSheet.Copy After:=ActiveSheet
    ActiveSheet.Name = tempr
    End If
 End Sub
Your solution worked exactly as I needed. Thanks very much for your assistance.
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,492
Members
448,967
Latest member
visheshkotha

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