The SUBTOTAL function with function number 9 calculates the sum of visible cells in a range, ignoring any rows hidden by a filter.
Background
The SUBTOTAL function in Microsoft Excel is designed to perform calculations on data while respecting filters applied to a worksheet. Unlike regular functions such as SUM, which include all cells in a range regardless of visibility, SUBTOTAL can be configured to ignore rows hidden by filtering. This makes it particularly useful in dynamic reports and dashboards where users frequently apply filters to analyze subsets of data. The function was introduced to address the limitation of standard aggregation functions in filtered lists, ensuring that summary values update automatically as filters change. Function numbers 1 through 11 include hidden rows, while numbers 101 through 111 exclude them. Function number 9 corresponds to SUM and includes hidden rows, whereas 109 corresponds to SUM and excludes hidden rows. Understanding this distinction is key to using SUBTOTAL correctly in filtered environments.
How It Works
When SUBTOTAL is used with function number 9, it sums all numeric values in the specified range, including those in rows hidden by manual hiding or filtering. However, if the goal is to sum only visible cells after applying a filter, function number 109 should be used instead. The syntax is SUBTOTAL(function_num, ref1, [ref2], ...), where function_num determines the operation and whether hidden values are included. For example, =SUBTOTAL(9, B2:B100) returns the sum of B2:B100, including any rows hidden by filtering. In contrast, =SUBTOTAL(109, B2:B100) sums only the visible cells in that range after filtering. This behavior occurs because SUBTOTAL with function numbers 101-111 is designed to ignore rows hidden by a filter, while 1-11 include them. It's important to note that SUBTOTAL always ignores other SUBTOTAL functions in the range to prevent double-counting in nested subtotals.
How to Choose Between Function Numbers
Choosing the correct function number depends on whether you want to include or exclude filtered-out data. Use function numbers 1-11 when you need to include manually hidden rows (e.g., rows hidden via right-click > Hide) in your calculation. Use 101-111 when you want to exclude both filtered and manually hidden rows. For summing visible data in a filtered table, 109 is typically the correct choice. For example, if you have a sales table filtered to show only 'West' region, and you want the total sales of visible rows, use =SUBTOTAL(109, sales_column). If you manually hide some of those visible rows and still want them included in the sum, use 9 instead. Misunderstanding this distinction is a common source of error, especially when users expect SUBTOTAL(9) to behave like a filtered sum.
Common Problems
One frequent issue is using SUBTOTAL(9) when the intention is to sum only visible filtered data, leading to inflated totals that include hidden rows. Another problem occurs when users apply SUBTOTAL within a filtered range and expect it to ignore filtered-out values, but mistakenly use function number 9 instead of 109. Additionally, SUBTOTAL does not ignore rows hidden by formatting (e.g., font color matching background) or grouping outlines—it only responds to filtering and manual hiding via the Hide command. Users may also encounter errors if the range includes non-numeric data; SUBTOTAL ignores text and blank cells but returns an error if the function number is invalid. Ensuring the correct function number and understanding the difference between filtering and manual hiding are essential for accurate results.
Comparisons
Compared to the SUM function, SUBTOTAL offers dynamic responsiveness to filters but requires careful selection of function numbers. SUM always totals all cells in a range, making it simpler but less adaptable to filtered views. AGGREGATE is a more flexible alternative that offers additional options, such as ignoring error values and nested subtotals, with finer control over what to ignore. However, SUBTOTAL remains widely used due to its simplicity and compatibility with older Excel versions. For filtered tables, SUBTOTAL(109) and AGGREGATE(9,5, range) both sum visible cells while ignoring filtered rows, but AGGREGATE can also ignore errors and hidden rows via its second argument. In unfiltered data, SUM, SUBTOTAL(9), and AGGREGATE(9,0,range) produce identical results.
Costs
There are no financial costs associated with using the SUBTOTAL function in Excel. It is a built-in feature available in all versions of Microsoft Excel, including Excel for Microsoft 365, Excel 2021, Excel 2019, and earlier versions back to Excel 2000. No add-ins, subscriptions, or additional licenses are required to use SUBTOTAL. The function consumes minimal computational resources and does not affect file size significantly when used in formulas. Its availability is universal across platforms, including Windows, macOS, and Excel for the web, ensuring consistent behavior regardless of the device or subscription tier.
Safety
Using the SUBTOTAL function poses no safety risks, as it is a purely computational tool within a spreadsheet environment. It does not access external systems, modify system settings, or interact with hardware. The primary risk is logical error—returning an incorrect sum due to misunderstanding whether hidden rows are included. This could lead to misinterpretation of data in financial, analytical, or reporting contexts. To mitigate this, users should verify results by temporarily removing filters and comparing SUBTOTAL output with SUM. There are no privacy, security, or health implications associated with its use. As with any formula, ensuring correct cell references and function numbers prevents unintended outcomes.
Alternatives
Alternatives to SUBTOTAL for summing visible filtered data include the AGGREGATE function and using filtered views with SUMIF or SUMPRODUCT combined with SUBTOTAL or AGGREGATE for helper columns. AGGREGATE offers more granular control, such as ignoring error values and nested subtotals, with function number 9 and options like 5 to ignore hidden rows. For example, =AGGREGATE(9,5,B2:B100) sums visible cells while ignoring errors and hidden rows. Another approach is to use a helper column with =SUBTOTAL(3,[@column]) to flag visible rows (returns 1 if visible, 0 if hidden), then sum based on that flag. Power Query can also filter and sum data during import, providing a static but auditable result. However, for most users needing a simple, dynamic sum of visible filtered data, SUBTOTAL(109) remains the most straightforward solution.
Frequently Asked Questions
Does SUBTOTAL(9) ignore filtered rows?
No, SUBTOTAL(9) does not ignore rows hidden by a filter; it includes them in the sum. To exclude filtered rows, use SUBTOTAL(109) instead.
Can SUBTOTAL ignore errors like #N/A or #DIV/0!?
No, SUBTOTAL does not ignore error values. If the range contains errors such as #N/A or #DIV/0!, the function will return an error. Use AGGREGATE instead if error ignoring is required.
Does SUBTOTAL work with horizontal ranges?
Yes, SUBTOTAL works with horizontal ranges (e.g., =SUBTOTAL(109, A1:Z1)) and will sum only the visible cells if columns are hidden by a filter, though filtering columns is less common than filtering rows.
Why does my SUBTOTAL return zero when I filter?
This may happen if the visible cells contain no numeric values or if the function number is incorrect. Check that you are using 109 for visible-only sums and that the filtered cells contain numbers, not text formatted as numbers or blanks.
Is SUBTOTAL affected by grouped rows?
SUBTOTAL ignores other SUBTOTAL functions in the range to avoid double-counting, but it does not ignore rows hidden by grouping outlines unless they are also filtered or manually hidden. Grouping alone does not affect visibility for SUBTOTAL purposes.
Can I use SUBTOTAL in Excel tables?
Yes, SUBTOTAL works seamlessly with Excel tables (ListObjects). When used in a table’s total row, Excel automatically inserts SUBTOTAL with the appropriate function number based on the selected calculation, ensuring it respects filters applied to the table.