VBA & Macro Problems

Create Excel Worksheets for Each Day of a Selected Month and Year

Huma Ashraf ChHuma Ashraf Ch Nov 27, 2026 869 views

Question details

Create one Excel worksheet for each day in a selected month and year instead of always using the current year.

Create Excel Worksheets for Each Day of a Selected Month and Year
Product
Microsoft Excel
Device & OS
not provided
Scenario
A user wants to modify a VBA calendar macro so it accepts a year and generates daily worksheets for a chosen month.
Observed behavior
The existing calendar code uses the current year when creating its base date.
Before you start

Open the target workbook in a desktop version of Excel that supports VBA and save it as an Excel Macro-Enabled Workbook (.xlsm). Make sure macros are enabled according to your organization's security settings.

Solution 1Recommended

Prompt for the month and year in an Excel VBA macro

Use DateSerial with the selected year and month to create one worksheet per day, including the correct number of days for leap years.

The example creates sheets in the workbook containing the macro. It names them in YYYY-MM-DD format and skips names that already exist.

1
Open the VBA editor

In the target workbook, press Alt+F11 or select Developer > Visual Basic. In the VBA editor, select Insert > Module.

2
Add the macro

Paste this code into the module: Sub CreateDailyWorksheets() Dim selectedMonth As Variant, selectedYear As Variant Dim target As Workbook, existingSheet As Object Dim daysInMonth As Long, dayNumber As Long Dim sheetName As String selectedMonth = Application.InputBox("Enter a month number (1-12):", "Select month", Type:=1) If VarType(selectedMonth) = vbBoolean Then Exit Sub selectedYear = Application.InputBox("Enter a year (1900-9998):", "Select year", Type:=1) If VarType(selectedYear) = vbBoolean Then Exit Sub If selectedMonth < 1 Or selectedMonth > 12 Or selectedMonth <> Int(selectedMonth) _ Or selectedYear < 1900 Or selectedYear > 9998 Or selectedYear <> Int(selectedYear) Then MsgBox "Enter a whole month from 1 to 12 and a whole year from 1900 to 9998." Exit Sub End If Set target = ThisWorkbook daysInMonth = Day(DateSerial(CLng(selectedYear), CLng(selectedMonth) + 1, 0)) For dayNumber = 1 To daysInMonth sheetName = Format(DateSerial(CLng(selectedYear), CLng(selectedMonth), dayNumber), "yyyy-mm-dd") Set existingSheet = Nothing On Error Resume Next Set existingSheet = target.Sheets(sheetName) On Error GoTo 0 If existingSheet Is Nothing Then target.Worksheets.Add(After:=target.Sheets(target.Sheets.Count)).Name = sheetName End If Next dayNumber End Sub

3
Run the macro

Return to Excel, select Developer > Macros, choose CreateDailyWorksheets, and select Run. Enter the month number and year when prompted.

Prompt for the month and year in an Excel VBA macro
Using an existing calendar macro: If your macro already has a month variable, replace Year(Now()) in its base date with the selected year. If user input is unnecessary, use a fixed year such as 2016 instead.
Free Microsoft Office alternative

Manage Calendar Workbooks with WPS Spreadsheet

WPS Office is a free, lightweight alternative with a familiar interface for opening and editing Excel workbooks. Its Excel format compatibility makes it easy to move calendar data between applications; VBA macro support may differ from Microsoft Excel.

Organize calendar data across worksheetsOpen and edit Microsoft Excel XLSX and XLS filesUse a familiar spreadsheet interface for an easier transitionWork with a free, lightweight office suite
microsoft office alternative - wps office

Frequently Asked Questions

How do I use a fixed year instead of prompting for one?

In your existing VBA code, replace Year(Now()) with the required year, such as 2016. Keep the selected month variable in the date expression.

Does the macro handle February in a leap year?

Yes. DateSerial calculates the last day of the selected month, so February has 29 days in a leap year and 28 otherwise.

What happens if a daily worksheet already exists?

The example checks the workbook's sheet names and skips any date whose name already exists. It does not overwrite that sheet.

Can I create sheets for an entire year at once?

The example prompts for one month at a time. Run it for each month, or modify the code to loop through month numbers 1 to 12 for the selected year.