VBA - Loop through all dates between 2 dates creating new sheets

Magoosball

Board Regular
Joined
Jun 4, 2017
Messages
70
Office Version
  1. 365
Hello

I have a start date in cell C7 and an end date in cell C9. I'm trying to write a macro that will create a new sheet for each day between the 2 date values and name the sheet the weekday of that date.

Example:
Cell C7 = 3/29/2019
Cell C9 = 4/2/2019
This should create 5 new sheets named "Friday", "Saturday", "Sunday", "Monday", "Tuesday"

Example 2:
Cell C7 = 4/2/2019
Cell C9 = 4/2/2019
This Should create 1 new sheet named "Tuesday"

Condition: If the date range between the start and end date is greater than 7 then create a message-box stating the start date can't span more than 1 week from the end date.


Thank you!!
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try:
Code:
Sub CreateSheets()
    Application.ScreenUpdating = False
    Dim x As Long
    If Range("C9").Value - Range("C7").Value > 7 Then
        MsgBox ("The start date can't span more than 1 week from the end date.")
        Exit Sub
    End If
    For x = Range("C7").Value To Range("C9").Value - 1
        Sheets.Add(After:=Sheets(Sheets.Count)).Name = Format(CDate(x), "dddd")
    Next x
End Sub
 
Upvote 0
Solution
Thank you for the prompt response. I was pretty dang close to getting it on my own ! I was able to get this working. I had to delete the "-1" from the end of "For x = Range("C7").Value To Range("C9").Value so it encompassed the end date."

Thanks again!
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,199
Members
453,022
Latest member
RobertV1609

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