I have a spreadsheet with a small Subroutine in it that should do three things when the tab for the sheet "Template" is clicked: 1. make a copy of the "Template" sheet and place it before the original "Template" sheet 2. change the name of the copied sheet to be today's date (10-13-2016) 3. change the contents of cell B1 to be today's date (Thursday, Oct 13, 2016)
The code listed below does these things sort of. The two things I need help on is this: 1. to get the sheet to copy I have to click another sheet and then click back on the "Template" sheet. I'd like to be able to just click the "Template" tab and have it create the copy, even if the "Template" sheet is already the active sheet. 2. for some reason the VBA code prevents me from deleting the tab that is created when you click the "Template" tab.
Private Sub Worksheet_Activate()
Application.EnableEvents = False
If ActiveSheet.Name = "Template" Then
Worksheets("Template").Copy before:=Worksheets("Template")
ActiveSheet.Range("B2").Select
ActiveCell.FormulaR1C1 = Format(Date, "dddd, mmm d, yyyy")
ActiveSheet.Name = Format(Date, "mm-dd-yyyy")
End If
Application.EnableEvents = True
End Sub
I know this is probably very simple but I haven't been able to find any reference to this behavior anywhere. Any and all help will be much appreciated.