Combination

Mini trial balance from journal lines with HSTACK, UNIQUE and SUMIFS

Build a list of accounts with their net postings from raw journal lines: UNIQUE gives the accounts, SUMIFS totals each one, HSTACK puts both side by side and SORT orders them.

Functions used:HSTACKUNIQUESUMIFSSORTExcel 2024 or later (This combination needs Excel 2024 or later)

Difficulty rating 4 of 5

Worked example

Net postings per account from the journal lines

Scenario

For the audit of Veldhuis Installatie BV you have exported four journal entries from the XAF auditfile, one row per line, with debit as a positive and credit as a negative amount. You want a mini trial balance per account to compare with the client's saldibalans, without building a pivot table.

The data

Sheet excerpt: F2 holds the formula
RowABCDEFGH
1Entry no.AccountDescriptionAmount
225-060145004500Rent October€3,200.00€ 3.200,0011001100=SORT(HSTACK(UNIQUE(B2:B9), SUMIFS(D2:D9, B2:B9, UNIQUE(B2:B9))))=SORT(HSTACK(UNIQUE(B2:B9); SUMIFS(D2:D9; B2:B9; UNIQUE(B2:B9))))€1,850.00€ 1.850,00
325-060116001600Rent October-€3,200.00€ -3.200,0013001300-€8,400.00€ -8.400,00
425-060211001100Customer receipt€8,400.00€ 8.400,0016001600-€3,200.00€ -3.200,00
525-060213001300Customer receipt-€8,400.00€ -8.400,0040004000€5,100.00€ 5.100,00
625-060340004000Salary run€5,100.00€ 5.100,0045004500€4,650.00€ 4.650,00
725-060311001100Salary run-€5,100.00€ -5.100,00
825-060445004500Utilities€1,450.00€ 1.450,00
925-060411001100Utilities-€1,450.00€ -1.450,00
10
Formula cell (F2)Spilled result

Formula in F2

=SORT(HSTACK(UNIQUE(B2:B9), SUMIFS(D2:D9, B2:B9, UNIQUE(B2:B9))))
=SORT(HSTACK(UNIQUE(B2:B9); SUMIFS(D2:D9; B2:B9; UNIQUE(B2:B9))))

Result: 1100 | €1,850.00 / 1300 | -€8,400.00 / 1600 | -€3,200.00 / 4000 | €5,100.00 / 4500 | €4,650.001100 | € 1.850,00 / 1300 | € -8.400,00 / 1600 | € -3.200,00 / 4000 | € 5.100,00 / 4500 | € 4.650,00

How the pieces fit together

Watch out: If some accounts are stored as text ("1100") and others as numbers, UNIQUE lists the same account twice and SUMIFS counts both spellings under each. Convert the account column to one type first.

The formula taken apart, from the inside out. Each part can be typed into its own cell to see what it returns.

  1. =UNIQUE(B2:B9)
    =UNIQUE(B2:B9)

    Lists each account once, in order of first appearance: 4500, 1600, 1100, 1300, 4000. The eight journal lines hold five different accounts.

  2. =SUMIFS(D2:D9, B2:B9, UNIQUE(B2:B9))
    =SUMIFS(D2:D9; B2:B9; UNIQUE(B2:B9))

    Because the criterion is a list of five accounts, SUMIFS returns five totals, one per account:

    • 4500 gives 3,200 + 1,450 = 4,650
    • 1600 gives -3,200
    • 1100 gives 8,400 - 5,100 - 1,450 = 1,850
    • 1300 gives -8,400
    • 4000 gives 5,100
  3. =HSTACK(UNIQUE(B2:B9), SUMIFS(D2:D9, B2:B9, UNIQUE(B2:B9)))
    =HSTACK(UNIQUE(B2:B9); SUMIFS(D2:D9; B2:B9; UNIQUE(B2:B9)))

    Places the account column and the totals column side by side: a table of 5 rows and 2 columns, still in order of first appearance (4500 first, 4000 last).

  4. =SORT(HSTACK(UNIQUE(B2:B9), SUMIFS(D2:D9, B2:B9, UNIQUE(B2:B9))))
    =SORT(HSTACK(UNIQUE(B2:B9); SUMIFS(D2:D9; B2:B9; UNIQUE(B2:B9))))

    SORT orders the table on its first column, so the accounts run from 1100 to 4500 like a trial balance:

    • 1100 = 1,850
    • 1300 = -8,400
    • 1600 = -3,200
    • 4000 = 5,100
    • 4500 = 4,650

    The five totals add up to 0, as they must when every entry balances.

Step by step

  1. UNIQUE(B2:B9) reads the accounts 4500, 1600, 1100, 1300, 4000, 1100, 4500, 1100 and keeps each once: 4500, 1600, 1100, 1300, 4000.
  2. SUMIFS runs once per account in that list:

    • 4500: 3,2003.200 + 1,4501.450 = 4,6504.650
    • 1600: -3,2003.200
    • 1100: 8,4008.400 - 5,1005.100 - 1,4501.450 = 1,8501.850
    • 1300: -8,4008.400
    • 4000: 5,1005.100
  3. HSTACK puts the accounts next to the totals: 4500 | 4,6504.650, 1600 | -3,2003.200, 1100 | 1,8501.850, 1300 | -8,4008.400, 4000 | 5,1005.100.
  4. SORT orders the rows on the account number, ascending: 1100, 1300, 1600, 4000, 4500, each with its total.
  5. The result spills from F2 over F2:G6. Check: 1,8501.850 - 8,4008.400 - 3,2003.200 + 5,1005.100 + 4,6504.650 = 0, so the journal lines balance.

Why it works

UNIQUE gives the list of accounts once, and the same list is used as the criterion of SUMIFS. A criterion that is a list makes SUMIFS return one total per item, in the same order, so the accounts and the totals line up row by row.

HSTACK joins them into a table and SORT puts it in trial balance order. When the journal changes, the table grows or shrinks by itself.

Watch out for

  • The spill range F2:G6 must be empty. Anything in those cells gives a #SPILL! error.
  • The formula only covers the lines in B2:D9. When the export gets longer, extend the ranges or use a table, otherwise lines are missing from your totals.

Variations

=SUMIFS(D2:D9, B2:B9, 1100)
=SUMIFS(D2:D9; B2:B9; 1100)

One account at a time: 8,4008.400 - 5,1005.100 - 1,4501.450 = 1,8501.850. Fine for a single account, but you type every account yourself and can miss one.

=HSTACK(SORT(UNIQUE(B2:B9)), SUMIFS(D2:D9, B2:B9, SORT(UNIQUE(B2:B9))))
=HSTACK(SORT(UNIQUE(B2:B9)); SUMIFS(D2:D9; B2:B9; SORT(UNIQUE(B2:B9))))

Sorts the accounts first and then totals them in that order. Same table, but the list of accounts is written twice and sorting only on the account is all it can do.

=SUMIF(B2:B9, 4500, D2:D9)
=SUMIF(B2:B9; 4500; D2:D9)

The older SUMIF for one account: 3,2003.200 + 1,4501.450 = 4,6504.650. It works in every Excel version, but gives one number per formula and no list of accounts.

All combinations