Sub topic 11
Adding a total row
This chapter assumes you can create a table, name it, and read basic structured references such as SalesTable[Amount]. It also assumes you have applied a filter with a header button. Here you will turn on the total row, change what each cell calculates, and learn why the total row behaves differently from a formula you type below the table.
What the total row is
The total row is an optional extra row at the bottom of a table. It is part of the table, but it is not a data row. It does not count as a record, it stays at the bottom as the table grows, and sorting and filtering leave it alone.
To show it, click any cell in the table and tick Total Row in the Table Design tab. The keyboard shortcut is Ctrl+Shift+T. The first column shows the label "Total". The last column shows a sum if it holds numbers. The other cells start empty.
Click a cell in the total row and a small drop-down arrow appears. The list offers these functions:
- None
- Average
- Count
- Count Numbers
- Max
- Min
- Sum
- StdDev
- Var
- More Functions...
The drop-down only appears for the cell you have selected. Choose one, and Excel writes a formula into that cell.
What the formula looks like
The total row formula is a SUBTOTAL formula, not a plain SUM. If you choose Sum for the Amount column, Excel enters this:
=SUBTOTAL(109,SalesTable[Amount])The first argument is a function number. SUBTOTAL uses it to choose the calculation. The numbers 101 to 111 ignore rows hidden by a filter or by Hide Rows. The numbers 1 to 11 ignore filtered rows but still include rows you hid by hand.
| Drop-down choice | Function number | Equivalent function |
|---|---|---|
| Average | 101 | AVERAGE |
| Count | 103 | COUNTA |
| Count Numbers | 102 | COUNT |
| Max | 104 | MAX |
| Min | 105 | MIN |
| StdDev | 107 | STDEV.S |
| Sum | 109 | SUM |
| Var | 110 | VAR.S |
Because the formula uses 109 and a structured reference, the total follows two rules. It covers only the data rows, and it leaves out rows hidden by a filter.
Totals and filtering
Compare the total row with an ordinary SUM:
=SUM(SalesTable[Amount])A SUM adds every row in the column, including rows a filter has hidden. The SUBTOTAL in the total row adds only the visible rows. If you filter the table to Region = "North", the total row shows the North total. A SUM placed elsewhere in the workbook still shows the grand total.
Use this to your advantage:
- Use the total row when you want a figure that changes with the filter.
- Use SUM (or SUMIFS) in a separate cell when you want a figure that never changes with the filter.
Changing the function for each column
Each column has its own total cell, and each can use a different function. A typical sales table might use:
- Sum for Amount
- Average for Unit Price
- Count for Order ID
To change one, click the total cell and pick a function from the drop-down. Choose More Functions... to open the Insert Function dialog. Excel then writes the function you pick, but it does not wrap it in SUBTOTAL, so check whether it respects filters.
You can also type a formula directly into a total cell. For example, this counts distinct values:
=SUMPRODUCT(1/COUNTIFS(SalesTable[Region],SalesTable[Region]))This one counts every row, including filtered ones, because COUNTIFS ignores filters. It also fails if the Region column contains blank cells, because it would divide by zero. A custom formula is allowed, but it is yours to maintain.
Referring to the total row in other formulas
You can read a total from outside the table with the [#Totals] item specifier:
=SalesTable[[#Totals],[Amount]]This returns the value in the total row for the Amount column. It is more reliable than a cell address such as D52, because the address changes when the table grows or when the total row is hidden.
If the total row is switched off, [#Totals] returns a #REF! error. A formula that depends on the total row therefore breaks when someone turns it off. If you share a workbook, tell people not to hide it, or read the number with a formula that does not rely on it, such as =SUBTOTAL(109,SalesTable[Amount]) in your own cell.
Other item specifiers work the same way:
| Specifier | Refers to |
|---|---|
| `[#All]` | Headers, data and total row |
| `[#Data]` | Data rows only |
| `[#Headers]` | The header row only |
| `[#Totals]` | The total row only |
| `[@]` | The current row (this-row reference) |
How the total row interacts with growth
When the total row is off, typing in the row directly below the table makes the table grow to include it. When the total row is on, that does not happen, because the row below is the total row. To add a record you have two options:
- Select the last cell of the last data row, which is the bottom-right data cell, and press Tab. Excel inserts a new data row above the total row.
- Right-click a data row and choose Insert, then Table Rows Above or Table Rows Below.
Dragging the small resize handle at the lower-right corner of the table also works, but you must drag over the total row's position. It is easier to use Tab.
Excel remembers the function you chose for each column when you toggle the total row off and on. This memory lasts as long as the workbook stays open and is saved with the file.
Common errors
- Total row shows the wrong number after filtering. The cell probably holds a SUM or a custom formula rather than SUBTOTAL. Choose the function again from the drop-down.
- A
#REF!error in a formula outside the table. The formula uses[#Totals], and the total row was turned off. - The total shows Count when you expected Sum. For text columns, Excel defaults to Count. Change it in the drop-down.
- A total of zero. The numbers are stored as text. Check for a green triangle in the corner of the cells, or left-aligned numbers, and convert them.
- The table will not grow when you type below it. The total row is on. Use Tab from the last data cell instead.
- The label "Total" has disappeared. You typed over it. It is only text, so type it back. The first column of the total row holds a label, not a formula, until you change it.
Turning the total row on with VBA
The ShowTotals property switches the total row on and off. You can also choose the function for each column through TotalsCalculation:
Sub ConfigureTotalRow()
Dim tbl As ListObject
Set tbl = ActiveSheet.ListObjects("SalesTable")
tbl.ShowTotals = True
tbl.ListColumns("Amount").TotalsCalculation = xlTotalsCalculationSum
tbl.ListColumns("Unit Price").TotalsCalculation = xlTotalsCalculationAverage
tbl.ListColumns("Order ID").TotalsCalculation = xlTotalsCalculationCount
End SubSetting TotalsCalculation writes the matching SUBTOTAL formula. Set it to xlTotalsCalculationNone to clear a cell. To read the total row as a range, use tbl.TotalsRowRange. If the total row is off, TotalsRowRange is Nothing, so test for that before you use it.
Summary of when to use the total row
| Situation | Best choice |
|---|---|
| A quick figure that follows the filter | Total row with Sum |
| A figure that must ignore filters | `SUM` or `SUMIFS` outside the table |
| A figure used by other formulas | `[#Totals]`, only if the total row stays on |
| Several different statistics | A different function per column |
Exercise
A table named OrdersTable has an Amount column. The total row shows =SUBTOTAL(109,OrdersTable[Amount]). You filter the table so only 3 of 10 rows are visible. A cell outside the table contains =SUM(OrdersTable[Amount]). Which of the two shows the total of all 10 rows, and what happens to the formula =OrdersTable[[#Totals],[Amount]] if someone turns the total row off?
Show answer
The SUM formula outside the table shows the total of all 10 rows, because SUM ignores filters. The total row's SUBTOTAL(109, ...) adds only the 3 visible rows. If the total row is turned off, =OrdersTable[[#Totals],[Amount]] returns a #REF! error, because the Totals part of the table no longer exists.
Exercise
Write a VBA procedure that turns on the total row of the table SalesTable on the active sheet and makes the Amount column show a sum.
Show answer
Sub TotalAmount() Dim tbl As ListObject Set tbl = ActiveSheet.ListObjects("SalesTable") tbl.ShowTotals = True tbl.ListColumns("Amount").TotalsCalculation = xlTotalsCalculationSum End Sub