Explanation

In this example, the goal is to count ages in column C according to the brackets defined in columns E and F. All data is in an Excel Table named data defined in the range B5:C16. A simple way to solve this problem is with the COUNTIFS function. If you are using Excel 365 or Excel 2021, another easy way to solve this problem is with the FREQUENCY function. Both approaches are explained below.

COUNTIFS function

The COUNTIFS function returns the count of cells that meet one or more criteria, and supports logical operators (>,<,<>,=) and wildcards (*,?) for partial matching. Conditions are supplied to COUNTIFS in the form of range/criteria pairs — each pair contains one range and the associated criteria for that range:

=COUNTIFS(range1,criteria1,range2,criteria2,etc)

Using this pattern we can count ages in brackets like this:

=COUNTIFS(data[Age],">=20",data[Age],"<=29") // 20-29
=COUNTIFS(data[Age],">=30",data[Age],"<=39") // 30-39
=COUNTIFS(data[Age],">=40",data[Age],"<=49") // 40-49

We are using the structured reference data[Age] for both ranges, since data is an Excel Table . Using an Excel Table means the table will expand automatically as new rows are added, and the counts will remain up to date.

Notice in the formulas above, we are hardcoding the numbers into the formula. This is generally considered a bad practice, since it’s easy to make a mistake when entering the formula and it’s more difficult to adjust the ranges later if needed, especially since each age bracket has its own unique formula. A better approach is to use ages already on the worksheet in columns E and F.

To do this, we need to concatenate the ages in columns E and F to the logical operators >= and <=. The final formula in G5 looks like this:

=COUNTIFS(data[Age],">="&E5,data[Age],"<="&F5)

Notice the operators are enclosed in double quotes ("") and attached to cell references E5 and F5 with an ampersand character (&). For a detailed overview of concatenation, see How to concatenate in Excel . As this formula is copied down, the reference to data[Age] behaves like an absolute reference and does not change, which means we can use the same formula for all five age brackets.

If you need a final bracket that captures all ages above 70, you can use a single condition like this:

=COUNTIFS(data[Age],">=70") // 70+

This formula will return a count of all ages greater than or equal to 70.

FREQUENCY function

If you are using Excel 365 or Excel 2021, where array formulas are native , a nice way to solve this problem is with the FREQUENCY function . The FREQUENCY function returns more than one value at a time and needs to be entered as a multi-cell array formula in Legacy Excel . In modern Excel, you can simply use a formula like this in cell G5:

FREQUENCY(data[Age],F5:F9)

Here, the data_array is given as data[Age] and the bins_array is given as F5:F9, the “End” values in column F. The FREQUENCY function performs the calculation and returns an array like this:

{3;3;1;3;2;0}

Each number represents a count, and the results automatically spill into multiple cells. Notice that by design, the FREQUENCY function always returns a count for one more bin than is provided. This is called the overflow bin, and represents the count of any values greater than the largest value in the bins_array . To suppress this last count, you can nest the FREQUENCY function inside the DROP function like this:

=DROP(FREQUENCY(data[Age],F5:F9),-1)

The DROP function will remove the last row in the array returned by FREQUENCY and the result will match the values returned by COUNTIFS above.

With a Pivot Table

A Pivot Table is another way to solve this problem. See this video for a similar example .

Explanation

In this example, the goal is to count numbers in the range B5:B15 ( named data ) that begin with the numbers shown in column D. You would think this would be a good problem to solve with the COUNTIF function but for reasons explained below, COUNTIF won’t work. Instead, you can use the SUMPRODUCT and Boolean logic . See below for a full explanation.

COUNTIF function

The COUNTIF function returns the count of cells that meet one or more criteria, and supports logical operators (>,<,<>,=) and wildcards (,?) for partial matching. You would think you could use the COUNTIF function with the asterisk wildcard () like this:

=COUNTIF(data,"25*") // returns 0

The problem however is using any wildcard character means criteria must be handled as a text value in double quotes ("") whereas the values in column B are TRUE numbers. As a result, COUNTIF will never find a matching number and the result will always be zero. You might also think of this trick to coerce the numbers in data to text by concatenating an empty string ("") to the range like this:

=COUNTIF(data&"","25*") // throws error

But this will throw an error, because COUNTIF is in a group of eight functions that require an actual range for range arguments. In other words, you can’t use an array operation in place of a range argument.

SUMPRODUCT function

Another way to solve this problem is with the SUMPRODUCT function , the LEFT function , and Boolean logic . To count numbers in data that begin with 25, we can use a formula like this:

=SUMPRODUCT(--(LEFT(data,2)="25")) // returns 5

Working from the inside out, the LEFT function is used to check all numbers in data like this:

LEFT(data,2)="25"

Because we give LEFT 11 numbers in the range B5:B15, LEFT returns an array with 11 results:

{"25";"25";"35";"45";"25";"45";"25";"45";"25";"35";"55"}="25"

We then compare each value to “25” to force a TRUE or FALSE result. Note that LEFT automatically converts the numbers to text, so we use the text value “25” for the comparison. The result is an array of TRUE and FALSE values:

{TRUE;TRUE;FALSE;FALSE;TRUE;FALSE;TRUE;FALSE;TRUE;FALSE;FALSE}

In this array, TRUE values correspond to numbers that begin with “25” and FALSE values represent numbers that don’t. We want to count these results, but first we need to convert the TRUE and FALSE values to 1s and 0s. To do this, we use a double negative (–).

--{TRUE;TRUE;FALSE;FALSE;TRUE;FALSE;TRUE;FALSE;TRUE;FALSE;FALSE}

The resulting array contains only 1s and 0s and is delivered directly to the SUMPRODUCT function:

=SUMPRODUCT({1;1;0;0;1;0;1;0;1;0;0})

With only a single array to process, SUMPRODUCT sums the items in the array and returns 5 as result. In this formula, note we are hardcoding the value “25” and the length of 2. To adapt the formula for the worksheet shown, where the numbers we want to count appear in column D, we use the LEN function and a reference to cell D5 like this:

=SUMPRODUCT(--(LEFT(data,LEN(D5))=D5))

The LEN function returns the number of characters in D5. This makes the formula more generic so it will support numbers of different lengths. Also note that the numbers in column D are entered as text values, since the result from LEFT will also be text. You can avoid this requirement by adapting the formula to coerce the value in D5 to text:

=SUMPRODUCT(--(LEFT(data,LEN(D5))=D5&""))

Here we concatenate an empty string ("") to the D5, which converts any number to text. This version of the formula will work when there are numbers in column D, or text values.

Note: In Excel 365 and Excel 2021 you can use the SUM function instead of SUMPRODUCT if you prefer. This article provides more detail .