Case 4: Create a sheet per month with VBA
A sports club keeps its cash book in a workbook with one sheet per month. At the start of the year someone adds twelve sheets by hand, names them and copies the header row. The macro does it in one go and, on the second run, knows which months already exist.
In short: Set ws = Worksheets.Add(After:=Worksheets(Worksheets.Count)) appends a sheet at the end, and ws.Name = Format(DateSerial(yr, m, 1), "mmmm yyyy") names it "January 2026". Because the new sheet is active afterwards, always refer to the setup sheet through a variable. If the name already exists, ws.Name = ... stops with run-time error 1004, so check first.
The situation
The Setup sheet holds the settings: the year in B1, the first and last month in B2 and B3, and the headers for the month sheets in row 6 (Date, Description, Amount, Receipt no). Anyone who wants to add a "Done" column in the autumn only enters it there. The macro reads everything from this sheet; nothing is hard-coded.
Task 1: add the month sheets
Task A sports club keeps its cash book on one sheet per month. Write a macro AddMonthSheets that adds a new sheet after the last one for every month from "From month" (B2) to "To month" (B3) of the year in B1. The sheet name is the month in full plus the year, so "January 2026". Every new sheet gets the headers from row 6 of the Setup sheet in row 1, as many columns as there are, in bold.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Year | 2026 | ||
| 2 | From month | 1 | ||
| 3 | To month | 12 | ||
| 4 | ||||
| 5 | Header row for the month sheets: | |||
| 6 | Date | Description | Amount | Receipt no |
Three things from the worksheets lesson work together here. First, After:=Worksheets(Worksheets.Count): without After, Excel puts the new sheet before the active one, and since that is always the one just added, the months come out backwards with December first. Second, the variable setup for the Setup sheet: after each Add the new sheet is active, and a Range("B1") without a sheet in front would read from it. Third, the number of headers comes from End(xlToLeft) in row 6. Copying a fixed A6:D6 passes the visible data but not the check with an extra column. The exercise then reports: "It works on the visible data but not when checked with other data. Are there fixed numbers, row numbers or results in the code? Read the values from the cells."
| Expression | Result |
|---|---|
Format(DateSerial(2026, 3, 1), "mmmm yyyy") | March 2026 |
Format(DateSerial(2026, 3, 1), "mmm yy") | Mar 26 |
MonthName(3) | March |
Format(DateSerial(2026, 3, 1), "yyyy-mm") | 2026-03 |
Format and MonthName write the month name in the language chosen in the Windows regional settings. An English setup gives "March 2026", a German one "März 2026". If a workbook is used in several countries, numbers are safer: Format(DateSerial(yr, m, 1), "yyyy-mm") gives "2026-03", which also sorts correctly. More in the dates lesson.
Task 2: only add missing months
In April, January to March are already there, with entries in them. The macro from task 1 would now stop when renaming a sheet to "January 2026"; Excel and the exercise report "Run-time error '1004': That name is already taken. Try a different one."
Task January to March already exist, with entries in them. Running the macro from task 1 now would end in an error, because a sheet name can only exist once. Write a macro AddMissingMonths that adds a sheet for each month from B2 to B3 (at the end, named like "April 2026", headers from row 6 of Setup, in bold) only if there is no sheet with that name yet. Existing sheets stay as they are. Finally a MsgBox shows "Sheets added: " followed by the number of new sheets. Check whether a sheet exists with a function of your own, SheetExists(sheetName As String) As Boolean.
Setup
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Year | 2026 | ||
| 2 | From month | 1 | ||
| 3 | To month | 6 | ||
| 4 | ||||
| 5 | Header row for the month sheets: | |||
| 6 | Date | Description | Amount | Receipt no |
January 2026
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Date | Description | Amount | Receipt no |
| 2 | 9 Jan 2026 | Membership fee Lewis | 60.00 | C-001 |
| 3 | 20 Jan 2026 | Balls for youth training | -84.90 | C-002 |
| 4 | ||||
| 5 |
February 2026
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Date | Description | Amount | Receipt no |
| 2 | 6 Feb 2026 | Membership fee Ahmed | 60.00 | C-003 |
| 3 | ||||
| 4 | ||||
| 5 |
March 2026
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Date | Description | Amount | Receipt no |
| 2 | 2 Mar 2026 | Gym hire March | -150.00 | C-004 |
| 3 | 13 Mar 2026 | Summer fair donation | 200.00 | C-005 |
| 4 | ||||
| 5 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
The check sits in a function of your own that returns True or False. In the macro it reads almost like a sentence: If Not SheetExists(sheetName) Then. The alternative with On Error Resume Next saves the function but needs care: put On Error GoTo 0 straight after it, or the macro swallows every other error too. More in the error handling lesson.
Common mistakes
Worksheets.Add without After
The sheets come out backwards. The exercise reports that the sheets are not right and shows yours running from December to January, with the Setup sheet last.
Reading the settings without a sheet
Range("B2").Value in the loop reads from the new, empty sheet after the first Add. The loop then stops early or does not run at all. Always use setup.Range("B2").
Invalid sheet names
A sheet name can be at most 31 characters long and cannot contain any of : \ / ? * [ ]. "03/2026" fails because of the slash. "March 2026" and "2026-03" are fine.
In real Excel
Instead of writing a header row cell by cell, you can copy a ready-formatted template sheet: Worksheets("Template").Copy After:=Worksheets(Worksheets.Count), then rename the active sheet. That brings column widths, formulas and print settings along. Save the workbook as .xlsm, or the macro is gone once you close it.
Check your result
- Task 1: 13 sheets, Setup followed by January 2026 to December 2026, each with four bold headers in row 1.
- Task 2: "Sheets added: 3"; April, May and June 2026 follow March 2026, and the entries in January to March are unchanged.
Back to case 3 or to the course overview.