How to fix spacing in comma-separated lists in Excel
Excel formula
=TEXTJOIN(", ",TRUE,TRIM(TEXTSPLIT(A2,",")))Split at the commas, trim each item and join them back: =TEXTJOIN(", ",TRUE,TRIM(TEXTSPLIT(A2,","))).
- Result on the example
- one result per row in
B2, filled down toB6 - Works in
- Excel 2024 and Microsoft 365
- Google Sheets
- Different formula, see below
See it work
The formula is already in B2, filled down to B6. Change any input and the result updates, or edit the formula to experiment.
How it works
TEXTSPLIT breaks the list at every comma, whatever spaces surround it. TRIM cleans each item, and TEXTJOIN puts them back with a consistent ", " between them. TRUE also drops empty items, so a double comma doesn't leave a gap.
=TEXTJOIN(", ",TRUE,TRIM(TEXTSPLIT(A2,",")))Result"red, blue, green"
A2"red,blue , green"The value in A2.
TEXTSPLIT(A2,","){1 row × 3 cols} "red", "blue ", " green"TEXTSPLIT: Splits text across columns, and rows, at the delimiters you give.- text
A2"red,blue , green" - col_delimiter
","
- text
TRIM(TEXTSPLIT(A2,",")){1 row × 3 cols} "red", "blue", "green"TRIM: Removes extra spaces, leaving single spaces between words.- text
TEXTSPLIT(A2,","){1 row × 3 cols} "red", "blue ", " green"
- text
TEXTJOIN(", ",TRUE,TRIM(TEXTSPLIT(A2,",")))"red, blue, green"TEXTJOIN: Joins text with a delimiter between items.- delimiter
", " - ignore_empty
TRUE - text1
TRIM(TEXTSPLIT(A2,",")){1 row × 3 cols} "red", "blue", "green"
- delimiter
In Google Sheets
Google Sheets needs a different formula: =JOIN(", ",ARRAYFORMULA(TRIM(SPLIT(A2,",")))). On the same data it gives the same result. Here it is running in Google Sheets mode.
Other ways to do it
Each one was run on the same example sheet.
| Formula | Result | When to use it |
|---|---|---|
| =TRIM(SUBSTITUTE(SUBSTITUTE(TRIM(A2)," ,",","),",",", ")) | red, blue, green, oak, walnut, birch, steel… | Works in every version: remove spaces before commas, put one after each, and let TRIM collapse the doubles. |
Common mistakes
- The SUBSTITUTE version keeps empty items: "a,,b" becomes "a, , b". The TEXTJOIN version drops them.
- Items that differ only in case or spelling ("Red", "red") stay separate. Clean them with LOWER or a lookup table.
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.
Fix spacing in comma-separated lists
B2, rewrite the list with exactly one comma and one space between items and no spaces at the ends. Fill down to B6.Questions
How do I also remove duplicate items?
Add UNIQUE, splitting into rows: =TEXTJOIN(", ",TRUE,UNIQUE(TRIM(TEXTSPLIT(A2,,",")))).
How do I use a semicolon instead?
Change both delimiters: =TEXTJOIN("; ",TRUE,TRIM(TEXTSPLIT(A2,","))).
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.