How to count comma-separated items in a cell in Excel
Formula (Excel and Google Sheets)
=IF(TRIM(B2)="",0,LEN(B2)-LEN(SUBSTITUTE(B2,",",""))+1)Count the commas and add 1, returning 0 for an empty cell: =IF(TRIM(B2)="",0,LEN(B2)-LEN(SUBSTITUTE(B2,",",""))+1).
- Result on the example
- one result per row in
C2, filled down toC8 - Works in
- Excel 2024 and Microsoft 365
- Google Sheets
- Same formula
See it work
The formula is already in C2, filled down to C8. Change any input and the result updates, or edit the formula to experiment.
How it works
A list of n items has n-1 commas. Removing the commas with SUBSTITUTE and comparing lengths gives the number of commas, and adding 1 gives the items. The IF handles the empty order, which has no commas but also no items.
=IF(TRIM(B2)="",0,LEN(B2)-LEN(SUBSTITUTE(B2,",",""))+1)Result3
B2"Desk, Chair, Lamp"The value in B2.
TRIM(B2)"Desk, Chair, Lamp"TRIM: Removes extra spaces, leaving single spaces between words.- text
B2"Desk, Chair, Lamp"
- text
TRIM(B2)=""FALSEThe = operator tests whether the two sides are equal.
LEN(B2)17LEN: Number of characters.- text
B2"Desk, Chair, Lamp"
- text
SUBSTITUTE(B2,",","")"Desk Chair Lamp"SUBSTITUTE: Replaces matching text.- text
B2"Desk, Chair, Lamp" - old_text
"," - new_text
""
- text
LEN(SUBSTITUTE(B2,",",""))15LEN: Number of characters.- text
SUBSTITUTE(B2,",","")"Desk Chair Lamp"
- text
LEN(B2)-LEN(SUBSTITUTE(B2,",",""))2The - operator subtracts.
LEN(B2)-LEN(SUBSTITUTE(B2,",",""))+13The + operator adds.
IF(TRIM(B2)="",0,LEN(B2)-LEN(SUBSTITUTE(B2,",",""))+1)3IF: Returns one value if a test is true, another if false.- logical_test
TRIM(B2)=""FALSE - value_if_true
0not used - value_if_false
LEN(B2)-LEN(SUBSTITUTE(B2,",",""))+13
- logical_test
In Google Sheets
The same formula works in Google Sheets: =IF(TRIM(B2)="",0,LEN(B2)-LEN(SUBSTITUTE(B2,",",""))+1). We ran it in Google Sheets mode on the same data and it returns the same results.
Other ways to do it
Each one was run on the same example sheet.
| Formula | Result | When to use it |
|---|---|---|
| =IF(TRIM(B2)="",0,COUNTA(TEXTSPLIT(B2,",")))Excel only | 3, 1, 0… | Excel 2024 and Microsoft 365: TEXTSPLIT splits the list into cells and COUNTA counts them. |
| =IF(TRIM(B2)="",0,COUNTA(SPLIT(B2,",")))Google Sheets | 3, 1, 0… | Google Sheets: SPLIT does the same job, and it skips empty items from doubled commas. |
Common mistakes
- A trailing comma ("Desk, Chair,") counts an extra, empty item with the comma method. Clean the list, or use the Sheets SPLIT version, which ignores empty parts.
- Without the IF, an empty cell returns 1.
Try it yourself
Same data, empty answer cell. Your formula is graded instantly, then re-checked on changed data, so a typed-in number won't pass.
Count comma-separated items in a cell
C2, count the comma-separated items in the order, returning 0 for an empty cell, then fill down to C8.Questions
How do I count items separated by semicolons or line breaks?
Change the separator: ";", or CHAR(10) for line breaks.
How do I count how many orders include a particular item?
Use COUNTIF with wildcards: =COUNTIF(B2:B8,"*Chair*").
Checked by running on 2026-10-30: every formula and result on this page was calculated by the Spreadsheet Practice formula engine, in both Excel and Google Sheets modes.