Add sheet, and auto name it.

cadandcode

Board Regular
Joined
Jan 21, 2023
Messages
125
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
I'm trying to manage daily reports. Im doing repetitive things such copy sheet and rename it.
Sheet names are dates. Like 02/03/23 (dd,mm,yy)
I want to add new sheet by copying last sheet and rename it adding 1 day to date like 02/03/23 to 03/03/23.
I tried to do it by macro but it renames new sheet adding (2) at the end of sheet name. It names 02/03/23 (2) but I need 03/03/23

Here is my macro I tried;

VBA Code:
Sub CopySheetDateYesterday()
'
' CopySheetDateYesterday Makro
'

'
    Sheets("02.02.23").Select
    Sheets("02.02.23").Copy After:=Sheets(1)
    Sheets("02.02.23 (2)").Select
    Sheets("02.02.23 (2)").Name = "03.02.23"
    Range("B40").Select
    ActiveCell.FormulaR1C1 = "2/2/2023"
    Range("B41").Select
End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Just name the first sheet with whatever date you want and start adding new sheets.
This will be pasted in workbook open module.
VBA Code:
Private Sub Workbook_NewSheet(ByVal Sh As Object)
             Dim d As Date
             d = Sheets(Sheets.Count - 1).Name
           Sh.Name = d + 1
            
End Sub
 

Attachments

  • 1674573283110.png
    1674573283110.png
    55.6 KB · Views: 11
Upvote 0
Just name the first sheet with whatever date you want and start adding new sheets.
This will be pasted in workbook open module.
VBA Code:
Private Sub Workbook_NewSheet(ByVal Sh As Object)
             Dim d As Date
             d = Sheets(Sheets.Count - 1).Name
           Sh.Name = d + 1
           
End Sub
Thank you but I would like to copy last sheet because it has the data. Copy last sheet + name it date+1
 
Upvote 0
Try this
VBA Code:
Sub Copy()
    Dim d As Date
    ActiveSheet.Copy after:=Sheets(Sheets.Count)
    d = Sheets(Sheets.Count - 1).Name
    ActiveSheet.Name = d + 1
    
End Sub
 
Upvote 0
Possibly this?

VBA Code:
Sub New_Sheet()
  Sheets(Sheets.Count).Copy After:=Sheets(Sheets.Count)
  With Sheets(Sheets.Count)
    .Name = Format(DateValue(Replace(Left(.Name, 8), ".", "/")) + 1, "dd.mm.yy")
  End With
End Sub
 
Upvote 0
Possibly this?

VBA Code:
Sub New_Sheet()
  Sheets(Sheets.Count).Copy After:=Sheets(Sheets.Count)
  With Sheets(Sheets.Count)
    .Name = Format(DateValue(Replace(Left(.Name, 8), ".", "/")) + 1, "dd.mm.yy")
  End With
End Sub
Now I will try this too.
Umm, have to say that that is what I exactly look like since shinigami's one make it from "02/02/23" to "03/02/2023".
Peter's version is exactly keeping format I use in name. Thank you guys for both.
 
Upvote 0
Might depend on your local date formatting but hat code did not work for me - error.
If that code didnt work for you, you might be right about version of excel or local zone differences. I agree.
 
Upvote 0

Forum statistics

Threads
1,214,793
Messages
6,121,619
Members
449,039
Latest member
Mbone Mathonsi

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