Interesting way to accommodate for date data types.
This is how I handled it:
Create a module and place the actual functionality below there. no point in having replicated data due to template copying.
Code:
Public Sub CreateNextTimesheet()
'Creates next Timesheet from Template
Dim wsPrevTimesheet As Worksheet, wsNewTimesheet As Worksheet, wsTemplate As Worksheet
Dim dDateFrom As Date, dDateTo As Date, sBuildDate As String, iMonth As Long, iYear As Long
Dim iLastDay As Long
'Create pointer to worksheets and add new one to list
Set wsTemplate = ActiveWorkbook.Worksheets("Template")
Set wsPrevTimesheet = ActiveWorkbook.Worksheets(wsTemplate.Index - 1)
wsTemplate.Visible = xlSheetVisible
wsTemplate.Copy Before:=wsTemplate
Set wsNewTimesheet = ActiveWorkbook.ActiveSheet
'Copy over the template with button. This way only 1 line of code is replicated.
wsTemplate.Visible = xlSheetHidden
dDateFrom = CDate(wsPrevTimesheet.Range("D2").Value) 'Use full date with year
iMonth = Month(dDateFrom)
iYear = Year(dDateFrom)
'Create Timesheet 1st to 15th
If Day(dDateFrom) = 1 Then
dDateFrom = dDateFrom + 15
iMonth = iMonth + 1
If iMonth > 12 Then
iMonth = 1
iYear = iYear + 1
End If
dDateTo = CDate(iMonth & "/01/" & iYear) - 1 'Subtract back 1 day, make Excel Date type do the work
'Create Timesheet 16th to end of month
Else
iMonth = iMonth + 1
If iMonth > 12 Then
iMonth = 1
iYear = iYear + 1
End If
dDateFrom = CDate(iMonth & "/01/" & iYear)
dDateTo = CDate(iMonth & "/15/" & iYear)
End If
'Get last day of month making Excel Date type do the work again.
'Formula starts with 25th of month, adds 9 days, then subtracts
'day of month which ends at last day of previous month.
iLastDay = Day(CDate(Month(dDateTo) & "/25/" & Year(dDateTo)) + 9 _
- Day(CDate(Month(dDateTo) & "/25/" & Year(dDateTo)) + 9))
'Set new timesheet values and make it the active sheet
With wsNewTimesheet
.Activate
.Name = Format(dDateFrom, "MM-DD-YYYY") & " to " & Format(dDateTo, "MM-DD-YYYY")
.Range("D2").Value = dDateFrom
If iLastDay < 31 Then .Range("111:116").EntireRow.Hidden = True
End With
End Sub
Then on the template sheet your button code would be:
Code:
Private Sub NextTimeSheet_Button_Click()
Call CreateNextTimesheet
End Sub
This may not be exactly what you want, but hopefully it points you in the right direction.