which returns 4, since there are 4 unique names in B5:B14. Note: Another way to count unique values is to use the COUNTIF function. This is a much simpler formula, but it can run slowly on large data sets. Working from the inside-out, the MATCH function is used to get the position of each item that appears in the data: The result from MATCH is an array like this: Because MATCH always returns the position of the first match, values that appear more than once in the data return the same position. For example, because “Jim” appears 3 times in the list, he shows up in this array 3 times as the number 1. This array is fed into FREQUENCY as the data_array argument. The bins_array argument is constructed from this part of the formula: which builds a sequential list of numbers for each value in the data: At this point, FREQUENCY is configured like this: FREQUENCY returns an array of numbers that indicate a count for each number in the data array, organized by bin. When a number has already been counted, FREQUENCY will return zero. This is a key feature in the operation of this formula. The result from FREQUENCY is an array like this: Note: FREQUENCY always returns an array with one more item than the bins_array. We can now rewrite the formula like this: Next, we check for values greater than zero (>0), which converts the numbers to TRUE or FALSE, then use a double-negative (–) to convert the TRUE and FALSE values to 1s and 0s. Now we have: Finally, SUMPRODUCT simply adds the numbers up and returns the total, which in this case is 4.
Handling blank cells
Empty cells in the range will cause the formula to return an #N/A error. To handle empty cells, you can use a more complicated array formula that uses the IF function to filter out blank values: Note: adding IF makes this into an array formula that requires control-shift-enter. For more information, see this page.
UNIQUE function in Excel 365
In Excel 365, the UNIQUE function provides a better, more elegant way to list unique values and count unique values. These formulas can be adapted to apply logical criteria. A pivot table is also an excellent way to list and count unique values.
Dave Bruns
Hi - I’m Dave Bruns, and I run Exceljet with my wife, Lisa. Our goal is to help you work faster in Excel. We create short videos, and clear examples of formulas, functions, pivot tables, conditional formatting, and charts.