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

ABCD
1EntryCost centerAmountBranch
2901Sales1,180.00North
3902Marketing455.00North
4903IT2,210.00North
5904Warehouse290.00North
6905Admin133.20North
7906Sales1,720.00South
8907Marketing610.00South
9908IT395.00West
10909Sales1,010.00West
11910Admin98.60West
12911Marketing240.00West
13912Warehouse64.00West

North

ABCD
1EntryCost centerAmount
21001Sales1,250.00
31002 Marketing480.00
41003IT 2,300.00
51004Admin
61005Warehouse315.50

South

ABCD
1EntryCost centerAmount
22001Marketing760.00
32002Sales 1,890.00
42003Warehouse
5

West

ABCD
1EntryCost centerAmount
23001IT412.00
33002 Admin128.40
43003Sales940.00
53004Marketing205.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.

ABCD
1EntryCost centerAmountBranch
21001Sales1,250.00North
31002Marketing480.00North
41003IT2,300.00North
51005Warehouse315.50North
62001Marketing760.00South
72002Sales1,890.00South
83001IT412.00West
93002Admin128.40West
103003Sales940.00West
113004Marketing205.00West

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

ABCD
1Cost centerAmount
2Sales4,080.00
3IT2,712.00
4Marketing1,445.00
5Warehouse315.50
6Admin128.40
7Total8,680.90

Budget

ABCD
1Cost centerBudget
2Marketing1,500.00
3Admin400.00
4Sales3,800.00
5Warehouse300.00
6IT2,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.

Course complete: on to the final test

The final test checks what you know from all six levels with a mix of tasks.

Take the final test

Write to us

Choose language

Select your preferred language