Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

SUBTOTAL Formula in Google Sheets: Step-by-Step Guide

· 4 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

The SUBTOTAL function in Google Sheets returns a sum, average, count, or other aggregate for a range while optionally ignoring rows hidden by a filter. It is the correct choice when you want totals that update as you filter data, unlike SUM which includes hidden rows. To use it, type =SUBTOTAL(function_code, range) where function_code is a number (e.g., 9 for SUM, 1 for AVERAGE) and range is the cells to aggregate. The steps below show how to apply it, common pitfalls, and fixes.

What You'll Need

  • A Google account with access to Google Sheets (desktop browser or mobile app).
  • A spreadsheet with data arranged in columns, ideally with a header row.
  • Basic familiarity with selecting cell ranges and entering formulas.

Step-by-Step

  1. Open your spreadsheet in Google Sheets. Ensure your data is in a contiguous range (no completely blank rows or columns inside the range you want to subtotal).
  2. Click the cell where you want the result. This is typically below a column of numbers or in a summary area.
  3. Type the SUBTOTAL formula. Start with =SUBTOTAL(. The first argument is a function code number. Use 9 for SUM, 1 for AVERAGE, 2 for COUNT, 3 for COUNTA, 4 for MAX, 5 for MIN, 6 for PRODUCT, 7 for STDEV, 8 for STDEVP, 10 for VAR, or 11 for VARP. For a simple sum that ignores filtered rows, use 9.
  4. Add a comma and select the range. For example, =SUBTOTAL(9, B2:B100) sums B2 through B100. You can type the range or click and drag to select it.
  5. Close the parenthesis and press Enter. The cell displays the aggregate for the visible rows in the range. If no rows are hidden by a filter, it equals the same result as SUM.
  6. Test with a filter. Apply a filter to your data (Data > Create a filter), then hide some rows using the filter dropdowns. The SUBTOTAL result should update to include only visible rows. Note: rows hidden manually (right-click > Hide row) are still included; only filter-hidden rows are excluded when using function codes 1-11. To also ignore manually hidden rows, use codes 101-111 (e.g., 109 for SUM).
  7. Copy the formula down or across if you need subtotals for multiple columns or groups. Relative references adjust automatically.

Menu labels and function codes are consistent across versions, but the filter interface may differ slightly on mobile. The steps above apply to the desktop browser version; on the mobile app, you can enter the same formula but filtering is done via the toolbar.

Common Mistakes

  • Using SUM instead of SUBTOTAL. SUM includes hidden rows, so filtered totals will be wrong.
  • Forgetting the function code. SUBTOTAL requires a numeric code as the first argument; omitting it causes an error.
  • Using the wrong code for hidden rows. Codes 1-11 ignore only filter-hidden rows; codes 101-111 ignore both filter-hidden and manually hidden rows. Choose based on your needs.
  • Including subtotal formulas inside the range. If your range contains other SUBTOTAL formulas, they will be double-counted. Keep subtotal rows outside the aggregated range.
  • Assuming SUBTOTAL works with all functions. It supports only the 11 functions listed; others like MEDIAN are not available.

Tips

  • Use =SUBTOTAL(109, range) when you want totals to reflect both filtered and manually hidden rows.
  • Combine SUBTOTAL with conditional formatting to highlight subtotal rows.
  • For nested subtotals (e.g., subtotals within groups), place each SUBTOTAL formula outside the range it aggregates to avoid circular references.
  • Use named ranges to make formulas easier to read and maintain.
  • Remember that SUBTOTAL recalculates automatically when filters change, so no manual refresh is needed.

Troubleshooting

If your SUBTOTAL returns an error, check that the function code is a number between 1 and 11 or 101 and 111. If the result seems incorrect, verify that the range does not include other subtotal cells and that you are using the intended code for hidden rows. If the formula does not update after filtering, ensure the filter is applied to the correct range and that the SUBTOTAL range covers the filtered rows. For #VALUE! errors, confirm all cells in the range are numeric or that the function code is valid. If you see #REF!, the range may have been deleted or moved.

Frequently Asked Questions

What is the difference between SUBTOTAL and SUM?

SUM adds all values in a range, including hidden rows. SUBTOTAL can ignore rows hidden by a filter (and optionally manually hidden rows) depending on the function code used.

Can I use SUBTOTAL with AVERAGE?

Yes, use function code 1 for AVERAGE (or 101 to also ignore manually hidden rows). For example, =SUBTOTAL(1, C2:C50).

Why does my SUBTOTAL include hidden rows?

You are likely using a function code between 1 and 11, which ignores only filter-hidden rows. Use 101-111 to ignore manually hidden rows as well.

Does SUBTOTAL work on Google Sheets mobile app?

Yes, you can enter the same formula in the mobile app. Filtering is available but the interface may differ from desktop.

Can I nest SUBTOTAL functions?

No, nesting SUBTOTAL inside another SUBTOTAL is not supported and will cause errors or incorrect results.

What happens if my range contains text?

SUBTOTAL ignores text for functions like SUM and AVERAGE, but COUNTA (code 3) counts non-empty cells including text. Choose the function code accordingly.