making specific dates in VBA generic so it can be applied continuously

Enzo_Matrix

Board Regular
Joined
Jan 9, 2018
Messages
113
I am creating an on-time delivery report and this is the code for one of the steps in my process
Code:
Sub NewDay()'
' NewDay Macro
' Button that initiates new day set up for on-time delivery reports
'
' Keyboard Shortcut: Ctrl+Shift+A
'
    Sheets("Feb 23, 2018").Select
    Sheets("Feb 23, 2018").Copy Before:=Sheets(4)
    Sheets("Feb 23, 2018 (2)").Select
    Sheets("Feb 23, 2018 (2)").Name = "Feb 24, 2018"
    Sheets("Feb 23, 2018").Select
    Range("B2").Select
End Sub

I am wondering if there is a way to make the dates in this code change automatically for today's current date
Would changing
Code:
Sheets("Feb 23, 2018 (2)").Name = "Feb 24, 2018"
' to Sheets("Feb 23, 2018 (2)").Name = Today()
achieve my goal of having the name of the sheets change on their own to the next day in the sequence?
 
Last edited:

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Looks like it would. Try manually changing the last sheet to something other than today's date and try it out, but it looks like it would, to me.
 
Upvote 0
Try
Code:
Sub NewDay() '
' NewDay Macro
' Button that initiates new day set up for on-time delivery reports
'
' Keyboard Shortcut: Ctrl+Shift+A
'
   Sheets(Format(Date, "mmm dd, yyyy")).Copy Before:=Sheets(4)
   ActiveSheet.Name = Format(Date + 1, "mmm dd, yyyy")
   Range("B2").Select
End Sub
 
Upvote 0
modified code to:
Code:
Sub NewDay()'
' NewDay Macro
' Button that initiates new day set up for on-time delivery reports
'
' Keyboard Shortcut: Ctrl+Shift+A
'
    Sheets(3).Select
    Sheets(3).Copy Before:=Sheets(4)
    Sheets(4).Select
    Sheets(4).Name = Today()
    Sheets(3).Select
    Range("B2").Select
End Sub

ran it and came up with error message: "Compile error: Sub or Function not defined". It then highlights the 'Today()' text as well as 'Sub NewDay()'.
 
Upvote 0
thanks for the advice Fluff,

Copied and pasted your code and came up with and error message:
Run-time error '9'"
Subscript out of range
 
Upvote 0
ok, do you have a sheet called Feb 23, 2018 & do you have 4 sheets in your workbook?
 
Upvote 0
I do have a sheet called Feb 23, 2018 and the only time there is a 4th sheet is when Feb 23, 2018 is duplicated and the date is changed to the next day (only weekdays are counted).

In the second Code, I changed the names in the sheets to 3,4 as they will continuously be in the same positions of the workbook
 
Upvote 0
Try this
Code:
Sub NewDay() '
' NewDay Macro
' Button that initiates new day set up for on-time delivery reports
'
' Keyboard Shortcut: Ctrl+Shift+A
'
   Sheets(Format(Date, "mmm dd, yyyy")).Copy Before:=Sheets(Sheets.Count)
   ActiveSheet.Name = Format(Date + 1, "mmm dd, yyyy")
   Range("B2").Select
End Sub
It will put the new sheet before the last sheet in the book
 
Upvote 0
Thank you Fluffy for that

It does work however the new sheets that are populated are named:
Feb 24, 2018
Feb 23, 2018 (2)
Feb 23, 2018 (3)......
 
Upvote 0
That's because you've already got a sheet for the 24th & you cannot have 2 sheets with the same name.
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,272
Members
452,628
Latest member
dd2

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