Case 2: month-end close with VBA
At the close you do not want to read 17 journal lines, you want one balance per account, and you want to know that the journal actually balances. A macro handles both: once with SumIf against a fixed list of accounts, once with a Dictionary that collects the accounts itself.
In short: WorksheetFunction.SumIf(accountColumn, account, amountColumn) totals the debits or credits of one account from the journal. Without a fixed list of accounts, use a Dictionary: balances(acct) = balances(acct) + debit - credit. Finally Round(totalDebit - totalCredit, 2) = 0 checks that the journal balances.
The situation
End of March. Accounting has exported the month's postings from the ledger as a journal: date, document number, account, description and an amount either as a debit or as a credit on each line. For the close meeting, finance needs a summary with debits, credits and balance per account, and before that, confidence that the export is complete.
The terms
| Term | Meaning |
|---|---|
| Journal | All posting lines in date order. A document has at least two lines. |
| Debit and credit | The two sides of every entry. For each document, total debits equal total credits. |
| Balance | Here debits minus credits per account. Positive for bank, receivables and expenses, negative for payables and revenue. |
| Account number | The numbers in the example (1000 Bank, 1100 Accounts receivable, 2000 Accounts payable, 4000 Sales) are simple examples, not a standard chart of accounts. |
Invoice INV-118 shows what a document looks like: 11,900.00 debited to accounts receivable, 10,000.00 credited to sales and 1,900.00 credited to service revenue for the installation. Debits and credits are equal, so the document balances.
Task 1: debits, credits and balance per account
Task The Journal sheet holds the journal lines for March: account in C, amount as a debit (E) or a credit (F). The Summary sheet lists the accounts. Write a macro SummarizeJournal that fills in, for every account on Summary, the total debits from the journal in C and the total credits in D (WorksheetFunction.SumIf), and the balance, debits minus credits, in E. Check tests with more journal lines.
Summary
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Account | Name | Debit | Credit | Balance |
| 2 | 1000 | Bank | |||
| 3 | 1100 | Accounts receivable | |||
| 4 | 2000 | Accounts payable | |||
| 5 | 4000 | Sales | |||
| 6 | 4100 | Service revenue | |||
| 7 | 5000 | Purchases | |||
| 8 | 5100 | Freight in | |||
| 9 | 6100 | Salaries | |||
| 10 | 6200 | Rent |
Journal
| A | B | C | D | E | F | |
|---|---|---|---|---|---|---|
| 1 | Date | Doc no | Account | Description | Debit | Credit |
| 2 | 2 Mar 2026 | 501 | 6200 | Rent March | 4,800.00 | |
| 3 | 2 Mar 2026 | 501 | 1000 | Rent March | 4,800.00 | |
| 4 | 5 Mar 2026 | 502 | 1100 | INV-118 Becker Plumbing | 11,900.00 | |
| 5 | 5 Mar 2026 | 502 | 4000 | INV-118 Becker Plumbing | 10,000.00 | |
| 6 | 5 Mar 2026 | 502 | 4100 | INV-118 Becker Plumbing | 1,900.00 | |
| 7 | 9 Mar 2026 | 503 | 5000 | BILL-4471 Holt Steel Supply | 6,250.00 | |
| 8 | 9 Mar 2026 | 503 | 5100 | BILL-4471 Holt Steel Supply | 1,187.50 | |
| 9 | 9 Mar 2026 | 503 | 2000 | BILL-4471 Holt Steel Supply | 7,437.50 | |
| 10 | 12 Mar 2026 | 504 | 1000 | Payment INV-112 | 5,355.00 | |
| 11 | 12 Mar 2026 | 504 | 1100 | Payment INV-112 | 5,355.00 | |
| 12 | 18 Mar 2026 | 505 | 1100 | INV-119 Hoffman Construction | 3,570.00 | |
| 13 | 18 Mar 2026 | 505 | 4000 | INV-119 Hoffman Construction | 3,000.00 | |
| 14 | 18 Mar 2026 | 505 | 4100 | INV-119 Hoffman Construction | 570.00 | |
| 15 | 25 Mar 2026 | 506 | 2000 | Payment BILL-4471 | 7,437.50 | |
| 16 | 25 Mar 2026 | 506 | 1000 | Payment BILL-4471 | 7,437.50 | |
| 17 | 31 Mar 2026 | 507 | 6100 | Salaries March | 14,200.00 | |
| 18 | 31 Mar 2026 | 507 | 1000 | Salaries March | 14,200.00 |
You know SumIf from the lesson on WorksheetFunction. What is new is that the ranges sit on a different sheet from the results. That is why every range has the sheet variable in front, as in the lesson on worksheets:
summary.Cells(r, "C").Value = WorksheetFunction.SumIf( _
journal.Range("C2:C" & jLast), summary.Cells(r, "A").Value, journal.Range("E2:E" & jLast))
The underscore at the end of the line continues the statement on the next line; you can also write it on one line. SumIf counts empty debit or credit cells as 0.
Task 2: collect the accounts and check the close
Task 1 relies on someone keeping the list of accounts up to date. If accounting posts to a new account, it is missing from the summary and nobody notices. A Dictionary is more robust: it adds each account the first time it appears and keeps a running balance.
Task This time the Summary sheet is empty except for the headers, so the macro collects the accounts itself. Write a macro CloseCheck that uses a Dictionary to work out the balance (debits minus credits) of every account in the journal and lists the accounts from A2 down in the order they first appear in the journal, with the balance next to them in B. Along the way it adds up all debits and all credits. Then it reports "Journal balances." if both totals agree to the cent (Round(…, 2)), otherwise "Debits minus credits: " followed by the difference using Format(…, "#,##0.00"). Check also tests a month with a posting error.
Summary
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Account | Balance | ||
| 2 | ||||
| 3 | ||||
| 4 | ||||
| 5 |
Journal
| A | B | C | D | E | F | |
|---|---|---|---|---|---|---|
| 1 | Date | Doc no | Account | Description | Debit | Credit |
| 2 | 2 Mar 2026 | 501 | 6200 | Rent March | 4,800.00 | |
| 3 | 2 Mar 2026 | 501 | 1000 | Rent March | 4,800.00 | |
| 4 | 5 Mar 2026 | 502 | 1100 | INV-118 Becker Plumbing | 11,900.00 | |
| 5 | 5 Mar 2026 | 502 | 4000 | INV-118 Becker Plumbing | 10,000.00 | |
| 6 | 5 Mar 2026 | 502 | 4100 | INV-118 Becker Plumbing | 1,900.00 | |
| 7 | 9 Mar 2026 | 503 | 5000 | BILL-4471 Holt Steel Supply | 6,250.00 | |
| 8 | 9 Mar 2026 | 503 | 5100 | BILL-4471 Holt Steel Supply | 1,187.50 | |
| 9 | 9 Mar 2026 | 503 | 2000 | BILL-4471 Holt Steel Supply | 7,437.50 | |
| 10 | 12 Mar 2026 | 504 | 1000 | Payment INV-112 | 5,355.00 | |
| 11 | 12 Mar 2026 | 504 | 1100 | Payment INV-112 | 5,355.00 | |
| 12 | 18 Mar 2026 | 505 | 1100 | INV-119 Hoffman Construction | 3,570.00 | |
| 13 | 18 Mar 2026 | 505 | 4000 | INV-119 Hoffman Construction | 3,000.00 | |
| 14 | 18 Mar 2026 | 505 | 4100 | INV-119 Hoffman Construction | 570.00 | |
| 15 | 25 Mar 2026 | 506 | 2000 | Payment BILL-4471 | 7,437.50 | |
| 16 | 25 Mar 2026 | 506 | 1000 | Payment BILL-4471 | 7,437.50 | |
| 17 | 31 Mar 2026 | 507 | 6100 | Salaries March | 14,200.00 | |
| 18 | 31 Mar 2026 | 507 | 1000 | Salaries March | 14,200.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
The first exercise in this lesson is free.
The check at the end is the first control in any close. Because every document must balance, total debits across all lines must equal total credits. If they do not, a line is missing from the export or an entry was posted on one side only. The check data in this exercise contains exactly such an error: an account fee posted as a debit with no credit.
If Round(totalDebit - totalCredit, 2) = 0 Then
MsgBox "Journal balances."
Else
MsgBox "Debits minus credits: " & Format(totalDebit - totalCredit, "#,##0.00")
End If
Common mistakes
A range without its sheet
WorksheetFunction.SumIf(Range("C2:C" & jLast), …) looks at the active Summary sheet, which has no journal lines. Every total comes out as 0, and the exercise reports: "Summary!C2 contains 0.00, expected: 5,355.00. 19 more cells are different as well."
Adding credits instead of subtracting
If the Dictionary adds debits and credits together, you get turnover instead of balances. Bank then shows over 30,000 instead of -21,082.50.
Comparing without Round
If totalDebit = totalCredit Then often works with decimal amounts, but not always: computers do not store every decimal number exactly, and after many additions a tiny remainder is left. Rounding to cents keeps you safe.
In real Excel
- The accounts from task 2 are in the order they first appear. To sort them by account number afterwards, use
Sort, see the lesson on sorting. - Real exports often have thousands of lines. SumIf per account is still quick; faster still is reading the journal into an array once and working in memory.
- The same close with formulas is in the Excel month-end close case, as queries in the SQL month-end close case.
Check your result
- Task 1: Bank has 5,355.00 in debits and 26,437.50 in credits, balance -21,082.50. All balances add up to 0.
- Task 2: the first account on the summary is 6200 Rent with 4,800.00, because the journal starts with the rent. The message reads "Journal balances."
Back to case 1 or the course overview.