Adding a a new sheet with todays date... I have the code BUT

zb134

New Member
Joined
Jun 23, 2014
Messages
38
Code:
Sub CheckAndAddSheetNameAsCurrentDate()
Dim SheetName As String
SheetName = Format(Date, "dd-mm-yyyy") 'Change the format as per your requirement
 
On Error GoTo AddNew
 Sheets(SheetName).Activate
 
 Exit Sub
AddNew:
 Sheets.Add , Worksheets(Worksheets.Count)
 ActiveSheet.Name = SheetName
End Sub

This is what I use. Not my own code.
It works but ONCE a day. How do I change it so that if an additional new sheet is added a '-1' is appended to the end of the worksheet name.

Thanks!
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
This code will work as you requested and technically 'infinitely' throughout the day, although eventually you will run out of memory. ;)
First sheet is named as it already has been. Subsequent sheets will be named, "-1", "-2", "-3", ... , "-n"

Code:
Sub CheckAndAddSheetNameAsCurrentDate()
Dim SheetName As String
Dim i As Long
    SheetName = Format(Date, "dd-mm-yyyy") 'Change the format as per your requirement
    
    
    On Error GoTo AddNew
    For i = 1 To Worksheets.Count  'there cannot be more instances of a single date than there are sheets
        Sheets(SheetName).Activate
        SheetName = Format(Date, "dd-mm-yyyy") & "-" & i
    Next i
    
    Exit Sub
AddNew:
    Sheets.Add , Worksheets(Worksheets.Count)
    ActiveSheet.Name = SheetName
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,165
Messages
6,129,235
Members
449,496
Latest member
Patupaiarehe

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