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

TermMeaning
JournalAll posting lines in date order. A document has at least two lines.
Debit and creditThe two sides of every entry. For each document, total debits equal total credits.
BalanceHere debits minus credits per account. Positive for bank, receivables and expenses, negative for payables and revenue.
Account numberThe 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

ABCDE
1AccountNameDebitCreditBalance
21000Bank
31100Accounts receivable
42000Accounts payable
54000Sales
64100Service revenue
75000Purchases
85100Freight in
96100Salaries
106200Rent

Journal

ABCDEF
1DateDoc noAccountDescriptionDebitCredit
22 Mar 20265016200Rent March4,800.00
32 Mar 20265011000Rent March4,800.00
45 Mar 20265021100INV-118 Becker Plumbing11,900.00
55 Mar 20265024000INV-118 Becker Plumbing10,000.00
65 Mar 20265024100INV-118 Becker Plumbing1,900.00
79 Mar 20265035000BILL-4471 Holt Steel Supply6,250.00
89 Mar 20265035100BILL-4471 Holt Steel Supply1,187.50
99 Mar 20265032000BILL-4471 Holt Steel Supply7,437.50
1012 Mar 20265041000Payment INV-1125,355.00
1112 Mar 20265041100Payment INV-1125,355.00
1218 Mar 20265051100INV-119 Hoffman Construction3,570.00
1318 Mar 20265054000INV-119 Hoffman Construction3,000.00
1418 Mar 20265054100INV-119 Hoffman Construction570.00
1525 Mar 20265062000Payment BILL-44717,437.50
1625 Mar 20265061000Payment BILL-44717,437.50
1731 Mar 20265076100Salaries March14,200.00
1831 Mar 20265071000Salaries March14,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

ABCD
1AccountBalance
2
3
4
5

Journal

ABCDEF
1DateDoc noAccountDescriptionDebitCredit
22 Mar 20265016200Rent March4,800.00
32 Mar 20265011000Rent March4,800.00
45 Mar 20265021100INV-118 Becker Plumbing11,900.00
55 Mar 20265024000INV-118 Becker Plumbing10,000.00
65 Mar 20265024100INV-118 Becker Plumbing1,900.00
79 Mar 20265035000BILL-4471 Holt Steel Supply6,250.00
89 Mar 20265035100BILL-4471 Holt Steel Supply1,187.50
99 Mar 20265032000BILL-4471 Holt Steel Supply7,437.50
1012 Mar 20265041000Payment INV-1125,355.00
1112 Mar 20265041100Payment INV-1125,355.00
1218 Mar 20265051100INV-119 Hoffman Construction3,570.00
1318 Mar 20265054000INV-119 Hoffman Construction3,000.00
1418 Mar 20265054100INV-119 Hoffman Construction570.00
1525 Mar 20265062000Payment BILL-44717,437.50
1625 Mar 20265061000Payment BILL-44717,437.50
1731 Mar 20265076100Salaries March14,200.00
1831 Mar 20265071000Salaries March14,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."

Next: case 3, reconciliation

Tick the bank statement off line by line against the books and flag differences and open items.

Go to case 3

Back to case 1 or the course overview.

Write to us

Choose language

Select your preferred language