Filtering data in Excel only to find your formulas still calculate hidden rows is incredibly frustrating. While standard functions like SUM or AVERAGE are the go-to resources for basic datasets, the SUBTOTAL function grants you the power to dynamically ignore collapsed or filtered rows seamlessly. However, as a crucial stipulation, you must use the 100-series function codes-such as 109 instead of 9-to successfully exclude manually hidden rows. For example, using =SUBTOTAL(109, A2:A100) ensures only visible data is aggregated. Below, we will break down how to apply these formulas to master your dynamic reporting.
When working with large datasets in Microsoft Excel, filtering data is one of the most common tasks. However, a frequent source of frustration for Excel users is performing mathematical calculations-such as summing, counting, or averaging-on only the visible data after a filter has been applied.
By default, standard Excel functions like SUM, AVERAGE, and COUNT calculate across the entire referenced range, completely ignoring whether a row is visible or hidden. If you try to sum a column of filtered data using =SUM(A2:A100), Excel will return the total of all 99 rows, including the ones hidden by your filter.
To aggregate only visible cells, you need specialized functions designed to respect Excel's filtering states. In this guide, we will explore the two most powerful functions for this task-SUBTOTAL and AGGREGATE-and dive into advanced techniques like applying conditional logic (e.g., SUMIFS) to visible rows only.
For many years, the SUBTOTAL function has been the go-to solution for running calculations on filtered lists. It is highly versatile because it can perform 11 different operations (like sum, average, count, max, and min) depending on the "function number" you pass to it.
=SUBTOTAL(function_num, ref1, [ref2], ...)
The magic of the SUBTOTAL function lies in its first argument, function_num. This number dictates both the math operation and how the function handles hidden rows. There are two sets of function numbers:
| Operation | Includes Manually Hidden Rows (1-11) | Excludes Manually Hidden Rows (101-111) |
|---|---|---|
| AVERAGE | 1 | 101 |
| COUNT (numbers only) | 2 | 102 |
| COUNTA (text and numbers) | 3 | 103 |
| MAX | 4 | 104 |
| MIN | 5 | 105 |
| SUM | 9 | 109 |
Imagine you have a sales dataset in range C2:C100. To sum only the visible sales values after filtering by "Region" or "Product Class", use the following formula:
=SUBTOTAL(109, C2:C100)
If you filter the list to show only the "Midwest" region, this formula dynamically recalculates to display the sum of only those visible "Midwest" sales cells.
Introduced in Excel 2010, the AGGREGATE function is a vastly superior alternative to SUBTOTAL. Not only can it calculate visible cells, but it also has the unique ability to completely ignore error values (such as #N/A, #DIV/0!, or #VALUE!) within your dataset.
If your dataset contains a single error, a standard SUBTOTAL or SUM formula will break and return that same error. AGGREGATE solves this elegantly.
=AGGREGATE(function_num, options, ref1, [ref2], ...)
9 represents SUM, 1 is AVERAGE, and 3 is COUNTA. (Note: AGGREGATE supports up to 19 functions, including MEDIAN, MODE, and percentile calculations).3: Ignore hidden rows, error values, and nested SUBTOTAL/AGGREGATE functions.5: Ignore hidden rows only.6: Ignore error values only.7: Ignore hidden rows and error values.If you want to sum visible cells in C2:C100 while bypassing any potential cell errors in that column, use function number 9 (SUM) with option code 7 (ignore hidden rows and errors):
=AGGREGATE(9, 7, C2:C100)
This is the most bulletproof way to perform basic calculations on dynamic, real-world spreadsheet data.
A common roadblock is trying to perform a conditional sum (like SUMIFS) or a conditional count (like COUNTIFS) on filtered data. Since SUMIFS is programmed to scan the entire raw range regardless of visibility, standard formulas will fail to respect your active filters.
To overcome this, we can construct a specialized array formula combining SUMPRODUCT, SUBTOTAL, OFFSET, and ROW.
Suppose we have a dataset where column B contains "Category" and column C contains "Revenue". We have applied a filter to another column (e.g., "Country"), and now we want to sum the visible revenue in C2:C100, but only where the Category in column B equals "Software".
=SUMPRODUCT((B2:B100="Software") * SUBTOTAL(109, OFFSET(C2, ROW(C2:C100)-MIN(ROW(C2:C100)), 0, 1)))
(B2:B100="Software"): This evaluates every cell in column B. It returns an array of TRUE and FALSE values (e.g., {TRUE; FALSE; TRUE; ...}).OFFSET(C2, ROW(C2:C100)-MIN(ROW(C2:C100)), 0, 1): This is the clever trick. It breaks down the range C2:C100 into an array of individual single-cell references. Excel normally processes ranges as a single block; this construction forces Excel to look at each row individually.SUBTOTAL(109, ...): Because SUBTOTAL is fed an array of individual cell references from the OFFSET step, it evaluates the visibility of each row one by one. It returns a 1 if the row is visible, and a 0 if it is hidden.SUMPRODUCT(...): Finally, SUMPRODUCT multiplies these arrays together:
0, excluding it from the final sum. If the category does not match, it is multiplied by 0 (FALSE), also excluding it. Only visible rows matching "Software" are summed.
If the complex SUMPRODUCT/OFFSET formula feels too intimidating or slows down your workbook, you can use the highly efficient "Helper Column" method. This is often preferred in professional environments because it is easier to audit and troubleshoot.
D2:
=SUBTOTAL(103, C2)
(Note: We use 103 (COUNTA) pointing to a cell in the same row that is never empty. If the row is visible, the formula returns 1. If the row is hidden, it returns 0.)
SUMIFS or COUNTIFS by referencing your helper column. For example, to sum visible "Software" revenue:
=SUMIFS(C2:C100, B2:B100, "Software", D2:D100, 1)
This approach bypasses volatile functions like OFFSET, resulting in faster calculation speeds on large datasets with thousands of rows.
Disclaimer:
The documents and templates provided on this page are for informational and illustrative purposes only. They do not constitute professional, legal, or financial advice, and should not be relied upon as such. Because individual circumstances and regulatory requirements vary, these materials may not be suitable for your specific needs. We recommend consulting with a qualified professional before adapting or using any of these examples for official or commercial purposes.