Final project: a month-end close with macros
To finish the course you build a month-end close of the kind many finance and accounting teams run every month. Three branches send their entries, and you turn them into one clean list, a summary per cost center and a budget comparison. Each part is a macro of its own and draws on several levels of the course.
You will need: loops over sheets and rows from combine sheets, Trim from the string functions, the Dictionary, sorting, formatting, the structure from create a report, and a message with MsgBox.
The situation
An office supplies company has three branches: North, South and West. Each branch records its entries for the month on a sheet of its own, with an entry number, a cost center and an amount. The cost centers are Sales, Marketing, IT, Warehouse and Admin.
The lists are not quite clean. Some cost centers have spaces at the start or end because they were pasted from another system, and some entries have no amount yet because the invoice is still missing. Until now, someone copied everything together by hand each month, tidied it up and built a summary table. Three macros that run one after the other now take over that job.
The project
Part 1: combine and clean the entries
The first macro clears the Total sheet, collects the entries of every branch, strips stray spaces, leaves out entries without an amount and reports at the end how many it copied and skipped. That message is the log: whoever runs the macro sees at once whether entries are missing.
Task Part 1 of the month-end close. The macro CollectEntries should first clear all old rows below the header on Total. Then it takes every entry from each branch sheet into the next empty row on Total: entry number in A, cost center in B without leading or trailing spaces, amount in C, name of the branch sheet in D. Entries without an amount are not copied, only counted. At the end it reports, for example, "10 entries copied, 2 without an amount skipped."
Total
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Entry | Cost center | Amount | Branch |
| 2 | 901 | Sales | 1,180.00 | North |
| 3 | 902 | Marketing | 455.00 | North |
| 4 | 903 | IT | 2,210.00 | North |
| 5 | 904 | Warehouse | 290.00 | North |
| 6 | 905 | Admin | 133.20 | North |
| 7 | 906 | Sales | 1,720.00 | South |
| 8 | 907 | Marketing | 610.00 | South |
| 9 | 908 | IT | 395.00 | West |
| 10 | 909 | Sales | 1,010.00 | West |
| 11 | 910 | Admin | 98.60 | West |
| 12 | 911 | Marketing | 240.00 | West |
| 13 | 912 | Warehouse | 64.00 | West |
North
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Entry | Cost center | Amount | |
| 2 | 1001 | Sales | 1,250.00 | |
| 3 | 1002 | Marketing | 480.00 | |
| 4 | 1003 | IT | 2,300.00 | |
| 5 | 1004 | Admin | ||
| 6 | 1005 | Warehouse | 315.50 |
South
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Entry | Cost center | Amount | |
| 2 | 2001 | Marketing | 760.00 | |
| 3 | 2002 | Sales | 1,890.00 | |
| 4 | 2003 | Warehouse | ||
| 5 |
West
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Entry | Cost center | Amount | |
| 2 | 3001 | IT | 412.00 | |
| 3 | 3002 | Admin | 128.40 | |
| 4 | 3003 | Sales | 940.00 | |
| 5 | 3004 | Marketing | 205.00 |
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
Part 2: total per cost center
From the clean list, a new Summary sheet is built with one row per cost center, sorted by amount, with a grand total and consistent formats. Part 2 starts from a finished Total list, so you do not need to have solved part 1 first.
Task Part 2. Total holds the cleaned entries from part 1. The macro CostCenterSummary should add a sheet Summary after Total: "Cost center" and "Amount" in A1:B1, below that one row per cost center with the sum of its amounts, sorted by amount from largest to smallest. Then a row "Total" with the sum of all amounts. Header and total row (A and B) are bold, and every amount in column B has the number format #,##0.00. Collect the sums with a Dictionary.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Entry | Cost center | Amount | Branch |
| 2 | 1001 | Sales | 1,250.00 | North |
| 3 | 1002 | Marketing | 480.00 | North |
| 4 | 1003 | IT | 2,300.00 | North |
| 5 | 1005 | Warehouse | 315.50 | North |
| 6 | 2001 | Marketing | 760.00 | South |
| 7 | 2002 | Sales | 1,890.00 | South |
| 8 | 3001 | IT | 412.00 | West |
| 9 | 3002 | Admin | 128.40 | West |
| 10 | 3003 | Sales | 940.00 | West |
| 11 | 3004 | Marketing | 205.00 | West |
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
Part 3: compare against the budget
Finally, the budget comes in. The macro writes each cost center's budget and variance next to it, colors overruns red and reports which cost centers are over budget.
Task Part 3. Summary holds the totals per cost center from part 2, with the Total row at the bottom. The Budget sheet lists the budgets in a different order. The macro BudgetCheck should write "Budget" in C1 and "Variance" in D1 on Summary (both bold), and for every cost center (not for Total) put the budget in C and the variance, amount minus budget, in D, with C and D in the number format #,##0.00. If the variance is greater than 0, the cell in D gets a red font (vbRed). Finally it reports "Over budget: " followed by the names of those cost centers in table order, separated by a comma and a space (for example "Over budget: Sales, IT"), or "All cost centers within budget." if none is over. Look up the budgets with a Dictionary.
Summary
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Cost center | Amount | ||
| 2 | Sales | 4,080.00 | ||
| 3 | IT | 2,712.00 | ||
| 4 | Marketing | 1,445.00 | ||
| 5 | Warehouse | 315.50 | ||
| 6 | Admin | 128.40 | ||
| 7 | Total | 8,680.90 |
Budget
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Cost center | Budget | ||
| 2 | Marketing | 1,500.00 | ||
| 3 | Admin | 400.00 | ||
| 4 | Sales | 3,800.00 | ||
| 5 | Warehouse | 300.00 | ||
| 6 | IT | 2,500.00 |
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
What matters
Plan first, then write
Before writing code, jot the steps down as comments, for example ' 1. clear Total, ' 2. loop over sheets, ' 3. message. Then fill them in one at a time and run the macro after each step. In part 1 it helps to get the copying right first and add Trim and the amount check afterwards.
Row by row instead of whole blocks
In the combine-sheets lesson you copied whole blocks with Copy. Once every row has to be checked or changed, that no longer works. You need an inner loop over the rows and your own counter for the next empty row on Total.
Clean before you count
To the Dictionary in part 2, "IT" and "IT " are two different keys. If the space stays, IT shows up twice in the summary with two partial totals. That is why Trim belongs in part 1.
Sort first, then add the total
The total row is always the largest value. Write it before sorting and it moves to the top. So sort only the cost center rows and write the total row below them afterwards.
Checking for empty cells
To see whether an entry has an amount, use If ws.Cells(i, 3).Value = "" Then. An empty cell counts as empty text here.
If it doesn't work
- If part 1 reports that Total!B3 contains " Marketing" where "Marketing" was expected, the cost center needs
Trim. - If entries numbered 901 and up are still at the bottom of Total in part 1, the clearing step at the start is missing. Those rows are from last month.
- If the numbers in the message are off, check where you increase the counters: copied entries in the Else branch, skipped ones in the If branch. The text has to match the task exactly, full stop included.
- If "Total" ends up at the top in part 2, the total row was sorted along with the rest.
- If part 3 says "Summary!D7 should stay empty", the loop runs to the last row and calculates for Total as well. It should stop at
lastRow - 1. - If Check complains about the font color, look at the condition: red only for a variance greater than 0, and only the cell in column D.
- All three parts are also checked against other data, part 3 with a month where nothing is over budget. Fixed row numbers or names typed into the code show up there.
In real Excel
At work you would put the three macros in one module and add a fourth that calls them in turn, for example Sub MonthEndClose() running CollectEntries, CostCenterSummary and BudgetCheck. Before it starts, it asks with vbYesNo and makes a backup, as in enable macros and code safely. On a second run in the same month the Summary sheet already exists, so delete it first or add the month to the name. Save the workbook as .xlsm.
What you learned in this course
You can write macros with variables, conditions and loops, find the last row, work across several sheets, format cells, use worksheet functions, handle text and dates, copy, delete, sort and find data. On top of that come arrays, the Dictionary, error handling, your own procedures and functions, debugging, events and, finally, complete workflows like this month-end close. That covers most of the repetitive Excel work in a typical office.
FAQ
How many points is the project worth?
Each of the three parts is worth 15 points instead of the usual 10, because you solve several steps in one go.
Is the project free?
No. Projects from level 2 on are part of Premium. In every lesson of level 6 the first exercise is free, so you can prepare without Premium.
Is there only one right answer?
No. Check compares the result, meaning the sheets, cells, formats and messages after the run, not the text of your code. Whether you loop with For or For Each, or use With, is up to you. Parts 2 and 3 do need a Dictionary, because that is what the task is about.
What if I get stuck?
The hints below each task come in three steps: the idea, the structure, then almost finished code. Step through with F8 to see where your macro does something other than you expected. The lessons for each building block are linked at the top of this page.