SUBTOTAL Excel & Sheets
The SUBTOTAL function performs calculations on filtered data, ignoring hidden rows. Master 11 aggregate functions with practical examples and error solutions.
=SUBTOTAL(function_num, ref1, [ref2], ...)Quick Answer
SUBTOTAL function SUBTOTAL function is a powerful aggregate function in Excel and Google Sheets that performs calculations (like SUM, AVERAGE, COUNT, MAX, MIN) on filtered or grouped data while automatically excluding hidden rows. Unlike regular aggregate functions, SUBTOTAL dynamically updates when you apply filters, making it essential for dynamic reports and interactive dashboards.
=SUBTOTAL(function_num, ref1, [ref2])- ref1 - the primary data range to calculate
Practical Examples
Basic Sum of Filtered Sales Data
Calculate total sales for visible products in a filtered list
Average Excluding All Hidden Rows
Calculate average price excluding manually hidden and filtered rows
Count Visible Non-Empty Cells
Count how many customers are currently visible after applying filters
Maximum Value in Filtered Range
Find highest salary in currently visible departments
Nested Subtotals Without Double-Counting
Create department subtotals and grand total that automatically prevent double-counting
Multiple Non-Contiguous Ranges
Sum filtered data from multiple non-adjacent columns (e.g., Q1 and Q3 sales only)
Standard Deviation of Filtered Quality Data
Calculate standard deviation for quality control on filtered production batches
Dynamic Dashboard Summary
Create a dashboard header showing filtered record count and sum
Common Errors and Solutions
SUBTOTAL returns #VALUE! error
Invalid function_num parameter. Must be an integer between 1-11 or 101-111. Common causes: (1) Using function name instead of number (e.g., 'SUM' instead of 9), (2) Typo in function number (e.g., 12 or 112), (3) Cell reference for function_num contains text or error value, (4) Using decimal numbers like 9.5.
1. Verify function_num is a valid integer between 1-11 or 101-111 2. Use the number directly in the formula, not a text string 3. If using cell reference for function_num, ensure it contains a valid number 4. Common mappings: 9/109=SUM, 1/101=AVERAGE, 2/102=COUNT, 3/103=COUNTA, 4/104=MAX, 5/105=MIN 5. Check for extra spaces or hidden characters in formula
Always use numbers directly in formula or validate cell references. Create a dropdown list with valid function numbers (1-11, 101-111) if allowing user selection. Document which number corresponds to which function for team reference.
Example:
SUBTOTAL shows #REF! error
Reference error when range arguments are deleted, moved, or invalid. Common scenarios: (1) Referenced range or cells were deleted, (2) Sheet containing reference was deleted, (3) Circular reference created (formula cell included in its own range), (4) Range references exceed worksheet limits (>1,048,576 rows in Excel).
1. Check all range references exist and are accessible 2. Verify sheet names in cross-sheet references are correct 3. Use named ranges for stability (Insert > Name > Define) 4. Rebuild formula if references were permanently deleted 5. Use absolute references ($A$1:$C$100) to prevent accidental shifting 6. Check for circular references (Formulas > Error Checking > Circular References)
Use structured table references (Table1[Column]) or named ranges instead of direct cell references. These update automatically when data moves or is restructured. For cross-sheet references, use indirect references with named ranges for better stability.
Example:
SUBTOTAL includes manually hidden rows when it shouldn't
Using function_num 1-11 when you need 101-111. Numbers 1-11 exclude filtered rows but INCLUDE manually hidden rows (Format > Hide & Unhide > Hide Rows). This is by design but often misunderstood. Users expect all hidden rows to be excluded but get inconsistent results when some rows are manually hidden.
1. Add 100 to your function_num: 1ā101, 9ā109, 4ā104, etc. 2. Audit which rows are filtered vs manually hidden (View > Show > check Hidden Rows) 3. Use 101-111 range as default for predictable, consistent behavior 4. Document clearly whether formulas should include manually hidden rows 5. Test formula with both filter and manually hidden rows to verify behavior
Default to 101-111 range (e.g., 109 for SUM, 101 for AVERAGE) unless you specifically need to include manually hidden rows. Create team guidelines specifying which range to use. Add comments to formulas explaining the choice.
Example:
Division by zero in SUBTOTAL calculation
Occurs with statistical functions (STDEV, VAR) or AVERAGE when all rows are filtered or hidden, leaving zero visible cells. SUBTOTAL(107) trying to calculate standard deviation on empty set causes division by zero internally. Also happens when dividing SUBTOTAL result by zero or using SUBTOTAL result in another formula where all data is filtered.
1. Wrap SUBTOTAL in IFERROR for graceful handling: =IFERROR(SUBTOTAL(107,A:A), "No data") 2. Check if data exists before calculating: =IF(SUBTOTAL(103,A:A)>1, SUBTOTAL(107,A:A), "Insufficient data") 3. For statistical functions, ensure at least minimum data points (2+ for STDEV) 4. Validate filter settings aren't hiding all data 5. For AVERAGE, ensure at least one visible numeric value
Always use IFERROR wrapper for production formulas, especially with statistical functions: =IFERROR(SUBTOTAL(109,A:A), 0) =IFERROR(SUBTOTAL(107,A:A), "Need 2+ data points") Provide user guidance about minimum data requirements. Add conditional formatting to highlight when filters result in insufficient data.
Example:
SUBTOTAL returns 0 when expecting a value
All rows are hidden by filters, or all cells in range are empty/non-numeric for the specified function. For COUNT (2/102), only numeric values are counted. For SUM (9/109), text values are ignored and empty ranges return 0. May also occur if range reference is incorrect or data is on a different sheet.
1. Check filter settings - remove filters temporarily to see all data 2. Verify range reference is correct and includes data 3. Use COUNTA (3/103) instead of COUNT (2/102) if counting non-numeric values 4. Check data formatting - numbers stored as text aren't counted by COUNT 5. Use Formula Auditing (Formulas > Trace Precedents) to verify range
Add a validation check: =IF(SUBTOTAL(103,A:A)=0, "No visible data", SUBTOTAL(109,A:A)) Use conditional formatting to highlight when filters hide all data. Document expected behavior when no data is visible.
Example:
Best Practices and Advanced Tips
Always Use 101-111 Range for Consistency
Default to function numbers 101-111 (e.g., 109 instead of 9 for SUM) in all new formulas. This ensures SUBTOTAL excludes ALL hidden rows regardless of how they're hidden, providing predictable behavior. Only use 1-11 when you specifically need to include manually hidden rows. This prevents confusion and ensures consistent results across your workbook.
Combine with Structured Table References
Use SUBTOTAL with Excel Tables (Insert > Table) and structured references for maximum stability and readability. Formula like =SUBTOTAL(109, SalesTable[Revenue]) automatically adjusts when table rows are added or removed, and is immune to reference errors from moving data. Structured references also make formulas self-documenting and easier for team members to understand.
Create Dynamic Dashboard Headers
Use SUBTOTAL(103) to create dynamic record count displays in dashboard headers. Combine with TEXT and concatenation: ="Showing " & SUBTOTAL(103,A2:A1000) & " of " & COUNTA(A2:A1000) & " records | Total: $" & TEXT(SUBTOTAL(109,C2:C1000),"#,##0"). This updates instantly when users apply filters, providing real-time feedback about their current view.
Understanding Nested SUBTOTAL Behavior
SUBTOTAL automatically excludes other SUBTOTAL results in its range to prevent double-counting. This is powerful for creating department subtotals with a grand total, but can cause confusion if forgotten. When your range includes cells with SUBTOTAL formulas, those cells are ignored in the calculation. Regular SUM would include them, leading to double-counting. Document this behavior in complex workbooks.
Performance Optimization for Large Datasets
For datasets with 10,000+ rows, limit SUBTOTAL ranges to actual data instead of entire columns. Use =SUBTOTAL(109, A2:A10000) instead of =SUBTOTAL(109, A:A). This can improve recalculation speed by 3-5x on large sheets with many formulas. Even better, use Excel Tables which automatically define the exact data range. For very large datasets (>100,000 rows), consider using Power Pivot.
Use with Data Validation Dropdowns
Create user-friendly dashboards by combining SUBTOTAL with data validation dropdowns for function_num selection. Set up a dropdown with values like '109 - Sum', '101 - Average', '104 - Max', '105 - Min'. Use LEFT or VLOOKUP to extract the number from the selection. This allows users to change calculation types dynamically without editing formulas, perfect for flexible dashboards.
Testing Filter Behavior
Always test SUBTOTAL formulas with various filter combinations to ensure they behave as expected. Apply filters, manually hide rows, and verify results update correctly. Test edge cases like all rows filtered out (should handle gracefully with IFERROR). Create a test worksheet with known values to validate calculations before deploying to production reports.
Avoiding Common Mistakes
Never include the SUBTOTAL formula cell within its own range - this can cause circular references or unexpected results. When using multiple SUBTOTALs, place them outside or below your data ranges. Use absolute references ($A$2:$C$100) when copying formulas to prevent range shifting. Document which function numbers you're using and why for team clarity.
Need Help with SUBTOTAL Excel & Sheets?
Stop struggling with formula syntax. Use AskFormulas to generate validated formulas instantly with our AI-powered tool.
Example Excel formula:
Related Formulas
Master the AVERAGE function with practical examples and error solutions. Learn to calculate mean values and analyze data efficiently in Excel and Sheets.
The COUNT function tallies numeric values in a range, ignoring text and empty cells. Essential for data analysis and statistical calculations.
Master the SUM function with practical examples and error solutions. Learn how to add numbers, ranges, and multiple criteria efficiently.
Master the SUMIF function with practical examples and error solutions. Learn how to sum cells based on criteria with step-by-step tutorials.