You may be able to work with this and edit to suit your needs.
After receiving input for starting and ending dates, it copies the
scheme worksheet into weekly sheets named after the week number plus
starting and ending dates for the week, Sunday through Saturday.
A file is attached. When testing you can quickly delete all the sheets you've created
by (1) clicking on the first tab to be deleted, (2) scroll to the last tab to be deleted,
(3) hold down the shift key and click the last tab (4) right-click the last tab and choose Delete.
Sub Copy_WorkSheet()
'copy the scheme worksheet as new weekly sheets, beginning from startDate through endDate.
'sheet name to include the week number, week starting date, and week ending date.
Dim startDate As Date, endDate As Date, weekStart As Date
Dim day1 As String, wksName As String, strWeekNumber As String
Dim testDate As Date, inputDate As String
Dim response, msgText As String, isWorksheet As Boolean
Dim weekCounter As Integer, numberOfWeeks As Integer, j As Integer
On Error GoTo Error_Handler
Application.ScreenUpdating = False
day1 = "Sun"
inputDate = InputBox("Enter starting date as mm/dd/yyyy" & _
vbCrLf & "or enter nothing to cancel.", "Start Date Input")
If Len(inputDate) = 0 Then Exit Sub
If Not IsDate(inputDate) Or Len(inputDate) < 8 Then
MsgBox "Starting date " & inputDate & " is not a valid date. Exiting."
Exit Sub
End If
startDate = CDate(inputDate)
inputDate = InputBox("Enter ending date as mm/dd/yyyy" & _
vbCrLf & "or enter nothing to cancel.", "End Date Input")
If Len(inputDate) = 0 Then Exit Sub
If Not IsDate(inputDate) Or Len(inputDate) < 8 Then
MsgBox "Ending date " & inputDate & " is not a valid date. Exiting."
Exit Sub
End If
endDate = CDate(inputDate)
If endDate < startDate Then
MsgBox "Ending date must be later than starting date." & vbCrLf & vbCrLf & _
"You entered a starting date of " & startDate & " and the ending date " & endDate
Exit Sub
End If
weekStart = startDate
'calculate date of first day of starting week
Do While Format(weekStart, "ddd") <> day1
weekStart = weekStart - 1
Loop
numberOfWeeks = Application.WorksheetFunction.RoundUp(DateDiff("d", startDate, endDate) / 7, 0)
For j = 1 To numberOfWeeks
'use the follwing 2 lines of code if weeks are only to be numbered sequentially
'weekCounter = weekCounter + 1
'strWeekNumber = CStr(weekCounter)
'otherwise, use this line of code if week numbers are to be calculated from the date variable
strWeekNumber = Format(weekStart, "ww")
wksName = "Wk_" & strWeekNumber & "_" & Format(weekStart, "mmddyyyy") & _
"_" & Format(weekStart + 6, "mmddyyyy")
isWorksheet = Sheet_Exists(wksName)
If isWorksheet Then
msgText = "Worksheet " & wksName & " already exists." & vbCrLf & vbCrLf & _
"Click Yes to continue this macro, or No to stop."
response = MsgBox(msgText, vbYesNo, "")
If response <> 6 Then Exit Sub
End If
If Not isWorksheet Then
Sheets("scheme").Copy After:=Sheets(Sheets.Count) 'creates a new worksheet
Sheets(Sheets.Count).Name = wksName 'renames the new worksheet
End If
weekStart = weekStart + 7
Next
Worksheets(1).Select
Application.ScreenUpdating = True
Exit Sub
Error_Handler:
MsgBox "An unexpected error occurred." & vbCrLf & Err & vbCrLf & Err.Description
Exit Sub
End Sub
Function Sheet_Exists(sSheetName As String) As Boolean
'return True is worksheet exists
Dim oWksheet As Worksheet, tmpBool As Boolean
For Each oWksheet In ActiveWorkbook.Sheets
If oWksheet.Name = sSheetName Then
tmpBool = True
Exit For
End If
Next
Sheet_Exists = tmpBool
End Function
Bookmarks