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

TermMeaning in the report
BudgetThe planned cost of the cost center for the month, usually from the annual budget.
ActualThe cost actually posted in the month.
VarianceActual minus budget. For costs, a positive figure means more was spent than planned.
Variance %Variance divided by budget. The base is always the budget.
ThresholdThe 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.

ABCDEF
1Cost centerNameBudgetActualVarianceVar %
21000Admin18,500.0017,942.35
32000Production96,000.00101,873.20
43000Sales32,400.0035,910.80
54000Marketing14,800.0019,366.45
65000IT9,600.0011,260.00
76000Warehouse12,500.0010,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.

ABCDEFGHI
1Cost centerNameBudgetActualVarianceVar %StatusThreshold amount2,000
21000Admin18,500.0017,942.35-557.65-3.0%Threshold %10%
32000Production96,000.00101,873.205,873.206.1%
43000Sales32,400.0035,910.803,510.8010.8%
54000Marketing14,800.0019,366.454,566.4530.9%
65000IT9,600.0011,260.001,660.0017.3%
76000Warehouse12,500.0010,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%".

ABCDEF
1Cost centerNameBudgetActualVarianceVar %
21000Admin18,500.0017,942.35-557.65-3.0%
32000Production96,000.00101,873.205,873.206.1%
43000Sales32,400.0035,910.803,510.8010.8%
54000Marketing14,800.0019,366.454,566.4530.9%
65000IT9,600.0011,260.001,660.0017.3%
76000Warehouse12,500.0010,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").Value stops with run-time error 11 (Division by zero). In a real report, test If Cells(r, "C").Value <> 0 Then first 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%".

Next: case 2, month-end close

Summarize the journal by account on a close sheet and check that debits equal credits.

Go to case 2

Back to the course overview.

Write to us

Choose language

Select your preferred language