Instant digital downloadExcel and Google SheetsSecure card checkout

Excel and Google Sheets how-to

Count how many times each value appears in a column

A staff list with a department on every row, and you need to know how many people are in each department. Two short formulas build the count table, and it keeps up when the list changes.

ABCDE
1NameDepartmentDepartmentCount
2AnaSalesSales4
3BenITIT2
4CySalesHR1
5DeeHRFinance1
6EveIT
7FaySales
8GusFinance
9HalSales

Sample data: a staff list in A2:B9. D2 holds =UNIQUE(B2:B9) and fills D2:D5 by itself; E2 holds =COUNTIF($B$2:$B$9,D2), copied down to E5.

The formula

First list each department once. Click D2 and type:

=UNIQUE(B2:B9)

D2 to D5 fill with Sales, IT, HR and Finance, in the order each one first appears. Then count each one. Click E2 and type:

=COUNTIF($B$2:$B$9,D2)

E2 shows 4: four people are in Sales. Copy it down to E5 for IT 2, HR 1 and Finance 1. The four counts add up to 8, one for every person on the list.

  • $B$2:$B$9 is the column to count in. The dollar signs keep it still as you copy the formula down; without them the range moves down a row each time, and rows at the top drop out of the count.
  • D2 is the value to count, the department on this row.

UNIQUE is in Excel for Microsoft 365, Excel 2024, Excel 2021 and Google Sheets (Microsoft's UNIQUE page). In older Excel, type the department names in D yourself and keep the COUNTIF, or use the PivotTable further down.

One formula, A to Z, with room for new rows

In Excel for Microsoft 365 or Excel 2024, one formula builds the whole table:

=LET(d,SORT(UNIQUE(TOCOL(B2:B100,1))),HSTACK(d,COUNTIF(B2:B100,d)))

Type it in D2 and it fills two columns by itself: Finance 1, HR 1, IT 2, Sales 4, A to Z. The range runs to row 100, so people added below the list are counted with no change to the formula.

  • TOCOL(B2:B100,1) turns the column into one list, and the 1 skips the empty cells (the section after next shows why that matters).
  • UNIQUE keeps each department once and SORT puts them A to Z.
  • LET names that list d, COUNTIF counts every name in it, and HSTACK puts the names and their counts side by side.

TOCOL and HSTACK are in Excel for Microsoft 365 and Excel 2024, not Excel 2021 (Microsoft's TOCOL page). We tested this one for Excel only; in Google Sheets, use the two formulas above or the pivot table.

Most common first

To sort the table by the counts, largest first:

=LET(u,UNIQUE(TOCOL(B2:B100,1)),c,COUNTIF(B2:B100,u),SORTBY(HSTACK(u,c),c,-1))

This puts Sales 4 at the top, then IT 2, then HR and Finance with 1 each. c holds the counts, and SORTBY with -1 sorts the table by them from the largest down. Change -1 to 1 to put the smallest first.

Why the list ends in a 0

Point a plain UNIQUE at more rows than you use, and the empty cells come through too:

=UNIQUE(B2:B100)

This gives Sales, IT, HR, Finance and then a fifth row that shows 0. Rows 10 to 100 are empty, UNIQUE keeps one empty value like any other, and Excel shows it as 0. Counting that row gives 0 as well. The TOCOL(B2:B100,1) in the formulas above skips the empty cells, so the extra row never appears. With the two formula version, keep both ranges to the rows you use, B2:B9 here.

How many different values

To count the departments without listing them:

=ROWS(UNIQUE(TOCOL(B2:B100,1)))

That gives 4 in Excel for Microsoft 365 and Excel 2024. In any version of Excel, this older formula gives 4 on the filled rows:

=SUMPRODUCT(1/COUNTIF(B2:B9,B2:B9))

Each Sales row adds 1/4, each IT row 1/2, and HR and Finance 1 each, so every department adds up to exactly 1. One empty cell in the range breaks it with #DIV/0!: COUNTIF returns 0 for the empty cell, and 1 divided by 0 is an error. For a range with empty rows, use:

=SUMPRODUCT((B2:B100<>"")/COUNTIF(B2:B100,B2:B100&""))

That gives 4 again. In Google Sheets, =COUNTUNIQUE(B2:B9) counts the different values in one step (Google's COUNTUNIQUE page). For the opposite number, how many rows are extra copies, take the different values away from the rows: =ROWS(B2:B9)-SUMPRODUCT(1/COUNTIF(B2:B9,B2:B9)) gives 4 (8 people, 4 departments).

Values that appear only once, and values that repeat

To list the departments that appear exactly once, give UNIQUE a third part, TRUE:

=UNIQUE(B2:B9,,TRUE)

That gives HR and Finance. Microsoft's UNIQUE page says TRUE returns the values that "occur exactly once", and Google's UNIQUE page lists the same third part for Google Sheets. To count them instead, in any Excel:

=SUMPRODUCT((COUNTIF(B2:B9,B2:B9)=1)*1)

That gives 2. To count the departments that appear more than once (Sales and IT):

=SUMPRODUCT((COUNTIF(B2:B9,B2:B9)>1)/COUNTIF(B2:B9,B2:B9))

That gives 2 as well. Dividing by the count makes each repeating department add up to 1, so Sales counts once, not four times. To mark each repeated row in the list itself, see find duplicates with COUNTIF.

No formula: a PivotTable

In Excel, click any cell in the list and choose Insert > PivotTable, then OK. In the PivotTable Fields pane, drag Department into Rows, then drag it again into Values. Each department shows with "Count of Department" beside it: Microsoft's PivotTable page says a field Excel reads as text is shown as a count, not a sum. A PivotTable does not update by itself: after the list changes, right-click it and choose Refresh.

In Google Sheets, select the list and choose Insert > Pivot table, then Create. In the side panel, next to Rows click Add and pick Department, then next to Values click Add and pick Department again. If "Summarize by" shows anything other than COUNTA, change it to COUNTA (Google's pivot table page).

One value counted as two

Say a ninth person was typed in as "Sales " with a space at the end. To UNIQUE and COUNTIF that is a different value, so it shows up as a fifth department with its own count of 1, and Sales stays at 4. Microsoft's COUNTIF page warns that leading and trailing spaces give unexpected counts. To find how many cells have extra spaces:

=SUMPRODUCT((B2:B100<>TRIM(B2:B100))*1)

Here it gives 1. To fix them, put =TRIM(B2) in a spare column, copy it down and count that column instead: Sales goes back to one row with 5, and there are 4 departments again. To make the fix stick, copy the TRIM column and paste it over the original with Paste Special > Values.

Capitals are not a problem. The same COUNTIF page says it ignores upper and lower case, so =COUNTIF(B2:B9,"sales") still gives 4.

Free practice sheet

Try it on three everyday jobs: how many people work on each team in a cafe, how many different customers ordered and how many came back, and T-shirt sizes from a form with stray spaces to clean before you count. Each tab has yellow cells to fill in and a Check column that says Correct or Try again as you type, plus an Answers tab. One Excel file that opens in Google Sheets too; every tab works in any version of Excel. Free, no sign up. Sample data.

Download the practice sheet (.xlsx)

Templates with the math already done

Rather skip the formulas? These templates come with every formula built and checked, plus a filled-in sample.

Teaching this to a class or team?

Get one tip like this a month

A free email from VARS Digital with one Excel or Google Sheets fix, tested in both apps. No spam, unsubscribe any time.

By signing up you agree that VARS Digital, 10151 240 Street, Maple Ridge, BC V2W 0G9, Canada (inquiries@varsdigital.app) may email you its newsletter: Excel and Google Sheets tips, updates and news about our templates and lessons. We send a confirmation email first, and nothing else goes out until you click it. Every email has an unsubscribe link, and you can leave any time. Details in our store policies.

More how-tos