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.
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Name | Department | Department | Count | |
| 2 | Ana | Sales | Sales | 4 | |
| 3 | Ben | IT | IT | 2 | |
| 4 | Cy | Sales | HR | 1 | |
| 5 | Dee | HR | Finance | 1 | |
| 6 | Eve | IT | |||
| 7 | Fay | Sales | |||
| 8 | Gus | Finance | |||
| 9 | Hal | Sales |
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.
- Employee Schedule TemplatePlan your team's shifts week by week with hours, overtime and labour cost.
- Sales Tracker and Dashboard 2027Every sale in one workbook, with revenue, top products and top customers on a dashboard.
- Attendance TrackerDaily P, A, T and E marks for one class, with rates and absence flags.
Teaching this to a class or team?
More how-tos
- Find a price by its code with XLOOKUPType a product code, get its price back from your price list. One formula, no helper columns.
- Total each category with SUMIFSAdd up all the Fuel, all the Office, all the Meals: one formula per category, and more rules when you need them.
- Check two conditions at once with IF and ANDFree shipping only when the order is 50 or more and ships in Canada: one IF, one AND, done.
- Add leading zeros to numbersType 00021 and Excel or Google Sheets shows 21, because to them it is just the number 21. Here is how to get the zeros back, and keep them.
- Fix #DIV/0! in an averageAn average of nothing is a division by zero, so AVERAGE shows #DIV/0! until the first number arrives. One IF fixes it.
- Count rows that meet two conditions with COUNTIFSHow many of Ana's deals were 500 or more? COUNTIFS answers it in one cell, and takes as many rules as you need.
- Calculate percentage changeSales went from 400 to 500. That is a 25% increase, and one short formula works it out for every row.
- Calculate years of service from a start dateAna started on 15 March 2019. How many full years has she worked? DATEDIF counts them, and the same formula works out an age from a date of birth.
- Combine first and last name into one cellFirst names in one column, last names in the next, and you need "Ana Lopez" in one cell. The & sign joins them.
- Find duplicates in a list with COUNTIFA mailing list where some people signed up twice. One formula shows how many times each email appears, so the repeats stand out.
- Split a full name into first and last nameYour list has "Ana Lopez" in one cell and you need Ana and Lopez in two. Two short formulas find the space and split there.
- Total amounts by month with SUMIFSA list of dated expenses and you want one total per month. SUMIFS adds every amount between the first and the last day of the month.
- Count working days between two datesAna is off from 1 Oct to 14 Oct. How many working days is that? NETWORKDAYS counts Monday to Friday and leaves out the weekends.
- Hide #N/A in VLOOKUP with IFERRORYour order list has a code that is not in the price list, and VLOOKUP shows #N/A. IFERROR puts your own words there instead.
- Count cells that contain textA stock list where some items have a note and most do not. One COUNTIF with an asterisk counts the notes and skips the empty cells.
- Make a running total (cumulative sum)Monthly sales in one column, the total so far in the next. One formula, filled down.
- Round to 2 decimal places, up or downA 15% discount turns 12.99 into 11.0415. ROUND makes it 11.04 and keeps your totals right to the cent. ROUNDUP and ROUNDDOWN handle the times when nearest is the wrong answer.
- How long to $1,000,000? FV and NPERPut $500 a month away at an example 7% a year. FV says what it grows to, NPER says when it passes $1,000,000. Two formulas, no calculator site.
- Why your formula gives the wrong number: the order of operationsA2 holds 10 and A3 holds 4. What does =A2-A3*2 give? It is 2, not 12. The same rule explains most formulas that look right and give the wrong number.
- Why your SUM says 0, and how to fix itThe amounts are right there in the column, and the total says 0. They look like numbers, but the sheet has them stored as text, and SUM skips text.
- Fill handle not working? Copy a formula down fastYou wrote one formula and need it in every row. Here are the quick ways to copy it down, and what to check when the fill handle will not show up or the copies come out wrong.
- Calculate hours worked from start and end timesA shift from 9:00 to 17:30 is 8.5 hours, but subtract the times and the cell shows 8:30. One small change turns it into a number you can add up and multiply by a pay rate.
- IF a cell is blank, or not blankYou want a formula to do one thing when a cell is empty and something else when it is filled. Two double quotes do the job, and they also catch the blanks ISBLANK misses.
- Formulas not updating? Recalculate and refreshYou change a number and the total does not move. It is nearly always one of a handful of things, and each one takes seconds to fix once you know which.
- Standard deviation in Excel and Google SheetsTwo shops both average 40 orders a day. One sells 38 to 42 every day; the other swings from 25 to 55. The average cannot tell them apart. The standard deviation can.
- Stack several columns into one column with TOCOLNames in three columns, one for each day, and you need them in one list. One formula does it, and the list keeps up when the columns change.
- Long numbers turn into E+ or end in 0 in ExcelYou type a 12 digit order number and Excel shows 5.93774E+11. You type a 16 digit account number and the last digit turns into 0. The first one is only how the cell looks. The second is a real loss, and it has to be stopped before you type.
- Free pricing calculatorsPrice a job from your real costs and margin. No sign up.
- Free downloadsPractice sheets and printable pages, no email and no sign up.







