ShareYourGroupShareYourGroupExcel Formulas

Math functions

SUBTOTAL function in Excel

SUBTOTAL totals, counts or averages a list while ignoring rows hidden by a filter.

Filter a list to show only one region, and the SUM at the bottom still shows the total for every region. SUBTOTAL is the cure. It works out its answer from the rows you can see, so the total follows the filter.

The examples above were run without a filter, where SUBTOTAL gives the same answers as the ordinary functions. Its value shows when you filter the list.

Syntax

=SUBTOTAL(function_num, ref1, [ref2], ...)
ArgumentWhat it is
function_numA code for the calculation: 9 for sum, 1 for average, 2 for count of numbers, 3 for count of filled cells, 4 for max, 5 for min. Add 100 (109, 101 ...) to also ignore rows hidden by hand.
ref1, ref2, ...The ranges to work on.

Examples

Each result below is what Excel itself showed for the formula on the sample sheet.

Sample sheet: Twelve sales made by four salespeople in three regions.
ABCDEF
1DateSalespersonRegionProductUnitsAmount
205-Jan-2026AshaNorthNotebook201200
312-Jan-2026RaviSouthPen50500
420-Jan-2026MeenaNorthBag43000
502-Feb-2026AshaEastPen30300
614-Feb-2026KabirSouthNotebook15900
728-Feb-2026RaviNorthBag21500
803-Mar-2026MeenaEastNotebook402400
915-Mar-2026AshaNorthBag32250
1022-Mar-2026KabirSouthPen60600
1101-Apr-2026RaviEastNotebook251500
1218-Apr-2026MeenaNorthPen80800
1306-May-2026AshaSouthBag53750

The function codes

CodeCode that also ignores hand-hidden rowsCalculation
1101AVERAGE
2102COUNT (numbers)
3103COUNTA (filled cells)
4104MAX
5105MIN
6106PRODUCT
9109SUM

The codes 7, 8, 10 and 11 give standard deviations and variances.

Filtered rows and hidden rows

There are two ways a row can disappear from view, and the two sets of codes treat them differently.

If you are not sure which you need, use the 100 series. =SUBTOTAL(109,F2:F13) is the safe "total of what is visible".

It ignores other subtotals

A list often has a subtotal after each group and a grand total at the bottom. If the grand total were a SUM, it would count every group twice: once in the rows and again in the group subtotals. SUBTOTAL skips any other SUBTOTAL formulas inside its range. So you can write one SUBTOTAL per group and one over the whole column, and the grand total is right.

The Subtotal command on the Data tab writes these formulas for you after you sort the list by the grouping column.

In an Excel table

When a range is formatted as a table (Ctrl and T) and the Total Row is switched on, the total cell contains a SUBTOTAL formula. That is why totals in tables follow the filter buttons in the headings. The drop-down arrow in the total cell changes the code between sum, average, count and the rest.

Numbering visible rows

=SUBTOTAL(3,$B$2:B2) in the first data row, copied down, numbers the visible rows 1, 2, 3 without gaps, however the list is filtered. Code 3 counts filled cells, and the range grows by one row with each copy.

AGGREGATE

A newer function, AGGREGATE, does everything SUBTOTAL does and can also skip error values, which SUBTOTAL cannot. If a column contains #N/A here and there, =AGGREGATE(9,6,F2:F13) adds the rest.

Common mistakes

In Google Sheets

SUBTOTAL exists in Google Sheets with the same codes.

Related functions

Every result on this page was calculated in Microsoft Excel (version 16.0, build 20326) on 12 November 2026, not typed by hand.

This is an independent help site. It is not connected with or approved by Microsoft. Excel and Microsoft 365 are trademarks of Microsoft Corporation, named here only to say which program a formula is for. The people and figures in the sample sheets are made up.