Case 1: budget vs actual with VBA
The cost center report is the first thing finance gets asked for every month. You have a macro work out the variances, flag only the lines that really stand out and finish with a total row and a message.
In short: variance is actual minus budget, the variance percentage is variance divided by budget. A macro fills both in with a loop down to the last used row, flags with Abs(…) > limit only what breaks two thresholds at once, and writes a total row underneath.
The situation
Early April at a mid-sized manufacturer. Every month, finance gets the actual costs per cost center from accounting and sets them against the budget. Until now someone dragged the variance formulas down by hand, and whenever a cost center was added, the formulas no longer fitted. Now a macro should finish the report, however many rows it has.
The Report sheet has one row per cost center: number in A, name in B, budget in C and actual in D. The amounts carry no currency symbol and use the number format #,##0.00.
The terms
| Term | Meaning in the report |
|---|---|
| Budget | The planned cost of the cost center for the month, usually from the annual budget. |
| Actual | The cost actually posted in the month. |
| Variance | Actual minus budget. For costs, a positive figure means more was spent than planned. |
| Variance % | Variance divided by budget. The base is always the budget. |
| Threshold | The point from which a variance gets discussed. Usually one for the amount and one for the percentage. |
Task 1: variance in amount and percent
Task The March cost report is on the Report sheet: budget in C and actual in D for each cost center. Write a macro CalcVariance that fills in, for every cost center from row 2 down to the last used row, the variance (actual minus budget) in column E and the variance as a percentage of budget in F (variance divided by budget, no rounding). The number formats are already set. Check also runs a month with more cost centers.
| A | B | C | D | E | F | |
|---|---|---|---|---|---|---|
| 1 | Cost center | Name | Budget | Actual | Variance | Var % |
| 2 | 1000 | Admin | 18,500.00 | 17,942.35 | ||
| 3 | 2000 | Production | 96,000.00 | 101,873.20 | ||
| 4 | 3000 | Sales | 32,400.00 | 35,910.80 | ||
| 5 | 4000 | Marketing | 14,800.00 | 19,366.45 | ||
| 6 | 5000 | IT | 9,600.00 | 11,260.00 | ||
| 7 | 6000 | Warehouse | 12,500.00 | 10,180.40 |
The skeleton comes from the lesson on finding the last row: first the last used row in column A, then a loop from row 2.
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
For r = 2 To lastRow
Cells(r, "E").Value = Cells(r, "D").Value - Cells(r, "C").Value
Cells(r, "F").Value = Cells(r, "E").Value / Cells(r, "C").Value
Next r
Column F then holds the fraction, so -0.03 for Admin. The 0.0% number format of the column shows it as -3.0%. If you round in code, you lose precision for later calculations; rounding belongs in the format.
Task 2: flag only what matters
Nobody reads a report where every line is red. That is why finance only flags items that break both thresholds: the absolute variance is above the amount threshold and the absolute percentage is above the percentage threshold. Both thresholds live in cells, so management can change them without touching the code.
Task The report now has the variance (E) and the variance percentage (F) as formulas. Only items that break both thresholds should go on the agenda: the amount threshold is in I1, the percentage threshold in I2. Write a macro FlagVariances that writes "review" in column G and fills the cell yellow (vbYellow) for every cost center where the absolute variance is greater than I1 and the absolute percentage is greater than I2, otherwise "ok" with no fill. Overspend and underspend count the same. Read the thresholds from the cells; Check tests with other thresholds and more cost centers.
| A | B | C | D | E | F | G | H | I | |
|---|---|---|---|---|---|---|---|---|---|
| 1 | Cost center | Name | Budget | Actual | Variance | Var % | Status | Threshold amount | 2,000 |
| 2 | 1000 | Admin | 18,500.00 | 17,942.35 | -557.65 | -3.0% | Threshold % | 10% | |
| 3 | 2000 | Production | 96,000.00 | 101,873.20 | 5,873.20 | 6.1% | |||
| 4 | 3000 | Sales | 32,400.00 | 35,910.80 | 3,510.80 | 10.8% | |||
| 5 | 4000 | Marketing | 14,800.00 | 19,366.45 | 4,566.45 | 30.9% | |||
| 6 | 5000 | IT | 9,600.00 | 11,260.00 | 1,660.00 | 17.3% | |||
| 7 | 6000 | Warehouse | 12,500.00 | 10,180.40 | -2,319.60 | -18.6% |
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.
You build the condition with And, as in the lesson on If Then Else. Abs returns the value without its sign, so a large underspend shows up too:
If Abs(Cells(r, "E").Value) > limitAmount And Abs(Cells(r, "F").Value) > limitPct Then
With thresholds of 2,000 and 10%, March flags Sales, Marketing and Warehouse. Production is almost 5,900 over budget, but that is only 6.1%; IT is 17.3% over, but only 1,660 in amount.
Task 3: total row with a message
Task The report needs a total row right below the last cost center, in the next empty row. Write a macro AddTotalRow: column A says "Total", C and D hold the totals of budget and actual (WorksheetFunction.Sum), E the actual minus budget of the totals and F E divided by C. The whole row from A to F is bold. Finally a MsgBox shows "Total variance: " followed by the percentage from F using Format(…, "0.0%"), for example "Total variance: 4.2%".
| A | B | C | D | E | F | |
|---|---|---|---|---|---|---|
| 1 | Cost center | Name | Budget | Actual | Variance | Var % |
| 2 | 1000 | Admin | 18,500.00 | 17,942.35 | -557.65 | -3.0% |
| 3 | 2000 | Production | 96,000.00 | 101,873.20 | 5,873.20 | 6.1% |
| 4 | 3000 | Sales | 32,400.00 | 35,910.80 | 3,510.80 | 10.8% |
| 5 | 4000 | Marketing | 14,800.00 | 19,366.45 | 4,566.45 | 30.9% |
| 6 | 5000 | IT | 9,600.00 | 11,260.00 | 1,660.00 | 17.3% |
| 7 | 6000 | Warehouse | 12,500.00 | 10,180.40 | -2,319.60 | -18.6% |
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.
For the totals you use WorksheetFunction.Sum from the lesson on WorksheetFunction. Work out the total percentage again from the totals; the average of the percentages above would be wrong, because Production weighs far more than IT. Format(value, "0.0%") turns the number into text such as 5.3% for the message.
Common mistakes
Looping to a fixed row
For r = 2 To 7 works for March but not for next month. The exercise then says: "It works on the visible data but not when checked with more or fewer rows. The loop stops at a fixed number: find the last row, e.g. lastRow = Cells(Rows.Count, 1).End(xlUp).Row."
Percentage based on actual
Dividing by column D instead of C gives different figures. The exercise reports: "F2 contains -3.1%, expected: -3.0%. 5 more cells are different as well."
Forgetting Abs
Without Abs the condition only catches overspend. Warehouse at -18.6% stays "ok", and the exercise says: "G7 contains "ok", expected: "review"."
Thresholds typed into the code
Abs(…) > 2000 matches the visible thresholds, but not once someone changes I1. Check tests with other thresholds and 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."
In real Excel
- A fill set by the macro stays until the macro runs again. If it only ever colors cells and never clears them, the old yellow is still there after a correction. Reset the fill of the column before the loop, for example with
Range("G2:G" & lastRow).Interior.ColorIndex = xlNone. - A budget of zero: if a cost center has no budget,
/ Cells(r, "C").Valuestops with run-time error 11 (Division by zero). In a real report, testIf Cells(r, "C").Value <> 0 Thenfirst and leave the percentage cell empty otherwise. - The same report with formulas is in the Excel budget vs actual case, straight on the database in the SQL budget vs actual case.
Check your result
- Task 1: Marketing is 4,566.45 or 30.9% over budget, Warehouse 2,319.60 or 18.6% under.
- Task 2: "review" appears for Sales, Marketing and Warehouse, all the others are "ok".
- Task 3: the total row is row 8 and the message reads "Total variance: 6.9%".
Back to the course overview.