Creating new worksheets that are named to be dates

miamidawgs

New Member
Joined
Apr 30, 2019
Messages
4
Hi, I am new to VBA and trying to create a form for a gas station. The form is made so a user can enter sales information and it will calculate month to date totals as pages are added through a new day button at the top. The button should create a new sheet that is named as the next day. The code I have works when u press it once but if it is pressed a second time it doesn't change the date. Is there a better way I can do this?
Code:
Private Sub CommandButton1_Click()
    Dim strTemplate As String: strTemplate = "C:\Users\Saif\Documents\Custom Office Templates\GasStationForm.xltm"
    Dim counter As Integer
    counter = counter + 1
    Sheets.Add After:=Sheets(Sheets.Count), Type:=strTemplate
    ActiveSheet.Name = Format(Date + counter, "YYYY-MM-DD")
End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
You try again:
Code:
Public counter As Integer


Private Sub CommandButton1_Click()
    Dim strTemplate As String
    strTemplate = "C:\Users\Saif\Documents\Custom Office Templates\GasStationForm.xltm"
    
    counter = counter + 1
    Sheets.Add After:=Sheets(Sheets.Count), Type:=strTemplate
    ActiveSheet.Name = Format(Date + counter, "YYYY-MM-DD")
End Sub
 
Upvote 0
How about
Code:
Private Sub CommandButton1_Click()
    Dim strTemplate As String: strTemplate = "C:\Users\Saif\Documents\Custom Office Templates\GasStationForm.xltm"
   [COLOR=#ff0000] Static counter As Long[/COLOR]
    counter = counter + 1
    Sheets.Add After:=Sheets(Sheets.Count), Type:=strTemplate
    ActiveSheet.Name = Format(Date + counter, "YYYY-MM-DD")
End Sub
 
Upvote 0
Which code are you referring to?
 
Upvote 0
Are you closing the workbook in between running the code?
 
Upvote 0
Glad you sorted it & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,171
Members
453,021
Latest member
Justyna P

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