Sub topic 11
Adding a total row
This chapter assumes you can already create and name a table, write structured references, and filter with the header buttons. Here you will add a total row, change what it calculates, and see why it behaves differently from an ordinary SUM.
What the total row is
The total row is an extra row at the bottom of a table. It stays attached to the table, so it moves down as rows are added. Each cell in it can show one summary of its column.
To turn it on, select any cell in the table. Then open Table Design and tick Total Row, or press Ctrl+Shift+T.
By default, Excel puts a result in the last column. It uses Sum for numbers and Count for text. The cell at the far left shows the label "Total".
Choosing the calculation
Click any cell in the total row. A drop-down arrow appears. The list includes None, Average, Count, Count Numbers, Max, Min, Sum, StdDev and Var. More Functions opens the full function list.
Select a function and Excel writes the formula for you. For a Sum on an Amount column, the cell contains:
=SUBTOTAL(109,SalesTable[Amount])Inside the table you may see [Amount] instead of the full table name. The two forms mean the same thing.
Why SUBTOTAL and not SUM
SUBTOTAL takes a function number as its first argument. The numbers 101 to 111 ignore rows hidden by a filter and also rows you hid by hand. The numbers 1 to 11 ignore filtered rows but still count rows hidden by hand.
| Function | Number used by the total row |
|---|---|
| Average | 101 |
| Count Numbers | 102 |
| Count | 103 |
| Max | 104 |
| Min | 105 |
| Sum | 109 |
Compare it with a plain sum:
=SUM(SalesTable[Amount])This formula always adds every row, whether or not a filter hides it. Use SUM when you want the grand total. Use the total row when the total should follow the filter.
Using the total from other cells
Other formulas can read a total row cell with the [#Totals] item specifier:
=SalesTable[[#Totals],[Amount]]This is useful for a dashboard cell that should show the filtered total. If you turn the total row off, this reference returns #REF!.
Adding rows with the total row on
Typing in the row directly below the total row does not extend the table. To add a row, click the last data cell, which is the bottom-right data cell, and press Tab. Excel inserts a new row above the total row. You can also right-click a row and choose Insert > Table Rows Above.
Common errors
- Total ignores a filter. The cell holds a
SUMyou typed, not theSUBTOTALthe drop-down creates. Re-select Sum from the drop-down. - Total is a count, not a sum. The column contains numbers stored as text. Fix the data, then re-select Sum.
- Custom formula disappears. You can type your own formula in a total row cell. If you clear the Total Row checkbox, those formulas are removed, and they may not return when you turn it back on.
- A total looks too small.
SUBTOTALskips otherSUBTOTALresults in the range, so nested totals are not double counted. Check that the range has no stray subtotal cells.
Total row in VBA
The ShowTotals property turns the row on or off. You can set a function for a column through its TotalsCalculation property:
Sub AddTotals()
Dim tbl As ListObject
Set tbl = ActiveSheet.ListObjects("SalesTable")
tbl.ShowTotals = True
tbl.ListColumns("Amount").TotalsCalculation = xlTotalsCalculationSum
End SubExercise
A table named SalesTable has a filter showing only the "North" region. Cell E20 holds =SUM(SalesTable[Amount]) and the total row holds =SUBTOTAL(109,[Amount]). Which cell shows the total for North only, and why?
Show answer
The total row cell. SUBTOTAL with function number 109 adds only the visible rows, so filtered-out rows are left out. The SUM formula in E20 adds every row regardless of the filter.