Case 6: report pack with VBA

Every cost center manager wants their own report, and management wants a summary of them all. One macro builds a sheet per cost center from the monthly data, a second one pulls every sheet together on a summary with a total row.

In short: Set ws = Worksheets.Add(After:=Worksheets(Worksheets.Count)) adds a sheet at the end, ws.Name = "CC " & number names it. From then on you reach every sheet through its variable, never through the active sheet. For the summary, For Each ws In Worksheets goes through all sheets and skips the summary itself.

The situation

The March figures sit on a Data sheet: cost center, cost type, budget and actual on each row. The Cost centers sheet lists the number and name of every cost center. Until now, finance copied a sheet for every cost center, filtered out the rows and pasted them in. A macro should take that over, and a second one should build a summary at the front.

The terms

TermMeaning
Report packA workbook with one sheet per cost center and a summary, rebuilt every month.
Cost typeThe kind of cost, such as personnel, occupancy or travel.
CCShort for cost center, used here in the sheet names.

Task 1: a report sheet per cost center

Task Every cost center gets its own report. Write a macro CreateReports that adds a new sheet after the last one for every cost center on the Cost centers sheet (from row 2) and names it "CC " plus the number, so "CC 1000". Each new sheet gets: in A1 "Cost report ", the number, a space and the name (for example "Cost report 1000 Admin"), bold; in A3:D3 the headers "Cost type", "Budget", "Actual", "Variance", bold; from row 4 down every row of that cost center from the Data sheet with cost type, budget, actual and variance (actual minus budget). B4 down to D of the last row get the number format #,##0.00.

Data

ABCD
1CCCost typeBudgetActual
21000Personnel42,000.0041,350.00
31000Occupancy6,150.006,150.00
41000Office supplies1,200.001,486.30
52000Personnel58,500.0060,120.00
62000Travel7,400.009,215.60
72000Advertising12,000.0010,880.00
83000Personnel24,000.0024,000.00
93000Software5,600.006,940.80
103000Hardware3,000.001,120.45
114000Personnel31,200.0030,960.00
124000Occupancy9,800.009,800.00
134000Packaging2,700.003,115.20

Cost centers

ABCD
1NoCost center
21000Admin
32000Sales
43000IT
54000Warehouse

How to add and name sheets is in the lesson on worksheets, how a report with header and formats comes together in create a report. The key point here: after Worksheets.Add the new sheet is active. Any Cells without a sheet in front now means the new, empty sheet. That is why every sheet has its own variable: dataWs, cc and ws.

For i = 2 To ccLast
    Set ws = Worksheets.Add(After:=Worksheets(Worksheets.Count))
    ws.Name = "CC " & cc.Cells(i, "A").Value
    ' write the header, then copy the matching rows from dataWs
Next i

The inner loop runs over all the data once per cost center and only takes the matching rows. outRow counts the rows on the new sheet from 4; after the loop the last row written is outRow - 1, and that is how far you apply the number format.

Task 2: the summary of all sheets

The workbook now has one sheet per cost center. The summary with budget, actual and variance per cost center is missing at the front. The macro does not know the sheets by name; it simply goes through all of them, as in the lesson on combining sheets.

Task The report pack has one sheet per cost center (cost types from row 4, budget in B, actual in C). The summary at the front is missing. Write a macro BuildSummary that goes through every sheet except "Summary" in order and writes one row per sheet on Summary from row 2 down: the sheet name in A, the total budget in B, the total actual in C and actual minus budget in D. If the variance is greater than 0, the cell in D gets a red font (vbRed). Below the last cost center goes a "Total" row with the totals of B, C and D, bold from A to D.

Summary

ABCD
1Cost centerBudgetActualVariance
2
3
4
5

CC 1000

ABCD
1Cost report 1000 Admin
2
3Cost typeBudgetActualVariance
4Personnel42,000.0041,350.00-650.00
5Occupancy6,150.006,150.000.00
6Office supplies1,200.001,486.30286.30

CC 2000

ABCD
1Cost report 2000 Sales
2
3Cost typeBudgetActualVariance
4Personnel58,500.0060,120.001,620.00
5Travel7,400.009,215.601,815.60
6Advertising12,000.0010,880.00-1,120.00

CC 3000

ABCD
1Cost report 3000 IT
2
3Cost typeBudgetActualVariance
4Personnel24,000.0024,000.000.00
5Software5,600.006,940.801,340.80
6Hardware3,000.001,120.45-1,879.55

CC 4000

ABCD
1Cost report 4000 Warehouse
2
3Cost typeBudgetActualVariance
4Personnel31,200.0030,960.00-240.00
5Occupancy9,800.009,800.000.00
6Packaging2,700.003,115.20415.20

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 total row adds up the summary, not all the sheets again. That way the single rows and the total are sure to agree, and the red font shows at a glance which cost centers are over budget.

Common mistakes

Cells without a sheet after Worksheets.Add

If Cells(r, "A").Value = … reads from the new, empty sheet instead of Data. No row matches, and the report sheets stay empty apart from the header.

Adding the sheet at the front instead of the end

Worksheets.Add without After puts the sheet before the active one, and because the new sheet then becomes active, the order is reversed. The exercise reports: "The sheets are not right. Expected: Data, Cost centers, CC 1000, CC 2000, CC 3000, CC 4000. Yours: CC 4000, CC 3000, CC 2000, CC 1000, Data, Cost centers."

Counting the summary in itself

Without If ws.Name <> "Summary" the summary lists itself as its first row. The exercise reports: "Summary!A2 contains "Summary", expected: "CC 1000". 19 more cells are different as well."

In real Excel

  • If a sheet with that name already exists, ws.Name = … stops with a run-time error. If the macro runs every month in the same workbook, delete last month's report sheets first or start a fresh workbook each time.
  • Sheet names can be at most 31 characters long and cannot contain certain characters such as / \ ? * [ ]. A number and a short prefix like "CC 1000" is safe.
  • With many cost centers, Application.ScreenUpdating = False at the start keeps the screen from jumping with every new sheet.

Check your result

  • Task 1: four new sheets, CC 1000 to CC 4000, follow Cost centers. CC 2000 lists Personnel, Travel and Advertising, with Travel 1,815.60 over budget.
  • Task 2: CC 2000 is 2,315.60 over budget and CC 4000 175.20, both in red. The total row shows 203,550.00 budget, 205,138.35 actual and 1,588.35 variance.

Done: the certificate

With this case you have worked through all six cases of the course. The course view in your account shows which tasks are still open and whether you still need a retake round, and that is where you apply for the "VBA for finance and accounting" certificate.

Back to the course overview

All six cases, the certificate rules and answers to common questions.

Go to the overview

Back to case 5.

Write to us

Choose language

Select your preferred language