Sub topic 10
Total only the visible rows with SUBTOTAL
This chapter assumes you can apply an AutoFilter, enter basic formulas, and tell a worksheet range from a column header. If a filter is new to you, read the AutoFilter chapters first.
Why SUM gives the wrong total
After you filter a list, Excel hides the rows that do not match. A formula such as =SUM(D2:D100) still adds every row in the range, hidden or not. The total no longer matches what you see on screen.
SUBTOTAL fixes this. It takes a function number that says which calculation to run, then a range. Rows hidden by a filter are always left out.
=SUBTOTAL(109, D2:D100)This sums column D and ignores rows hidden by a filter or hidden by hand, so the total matches what is visible.
Choosing the function number
The first argument picks the calculation. Each calculation has two numbers. The 1–11 set includes rows you hid by hand. The 101–111 set ignores them. Both sets skip rows hidden by a filter.
| Calculation | Include manually hidden rows | Ignore manually hidden rows |
|---|---|---|
| AVERAGE | 1 | 101 |
| COUNT | 2 | 102 |
| COUNTA | 3 | 103 |
| MAX | 4 | 104 |
| MIN | 5 | 105 |
| SUM | 9 | 109 |
Use the 100-series numbers when you want the total to match exactly what you can see. Use 1–11 only if you want manually hidden rows to stay in the result.
Worked example
Suppose A1:D100 holds a sales list. Row 1 has headers, and column D holds the amount. Put the total above the data, not below it, so new rows never push it out of view.
- In cell D1, leave the header "Amount" in place.
- In a cell above the table, such as D(-)...
Instead, insert two blank rows above the headers so the layout is clear:
- Insert two rows at the top, so the headers move to row 3 and the data runs from row 4 to row 102.
- In C1, type the label
Visible total. - In D1, enter the formula below.
=SUBTOTAL(109, D4:D102)- Turn on AutoFilter for the header row, then filter Region to West.
- Watch D1. It now shows the sum for West rows only. Clear the filter and it returns to the full total.
Keep one blank row between the total and the headers. Excel then does not treat the total as part of the filtered range.
Counting visible rows
The same function counts rows. Use 103 to count non-empty cells in a column, such as names, among the visible rows.
=SUBTOTAL(103, A4:A102)Filter the list and this number changes with it. It is a quick way to show "how many rows match" without reading the status bar.
Common errors
- Hidden rows still counted. You used
SUMor a number from 1–11 after hiding rows by hand. Switch to the 100-series number. - Total missing rows. The range does not reach the last data row. Extend it, or use a Table so the range grows by itself.
- Nested subtotals ignored. If the range holds other
SUBTOTALresults, Excel skips them to avoid double counting. This is useful, but it surprises people who expected those cells to be added. - Wrong direction.
SUBTOTALignores hidden rows, but 101–111 do not ignore hidden columns. Hiding a column does not change a vertical total.
Exercise
A list in A4:A102 has amounts in D4:D102. You filter by region and also hide two rows by hand. Which formula returns the sum of only the rows you can see, and why does =SUBTOTAL(9, D4:D102) give a different result?
Show answer
Use =SUBTOTAL(109, D4:D102). Function number 109 ignores rows hidden by a filter and rows hidden by hand. Number 9 ignores filtered rows but still includes the two rows hidden by hand, so its result is larger.
Exercise
Write a formula that shows how many rows are visible after filtering, using the names in A4:A102.
Show answer
=SUBTOTAL(103, A4:A102). Function 103 is COUNTA that ignores hidden rows, so it counts only the visible non-empty cells.