Excel and Google Sheets how-to
Remove extra spaces in Excel and Google Sheets with TRIM
Names that will not sort right, a VLOOKUP that says #N/A for a code you can see in the list, two cells that look the same but do not match: a space you cannot see is often the cause. TRIM fixes most of them, and one more step fixes the rest.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Name as typed | LEN | Clean name | LEN |
| 2 | Ana Silva | 11 | Ana Silva | 9 |
| 3 | Ben Okafor | 12 | Ben Okafor | 10 |
| 4 | Cara Liu | 11 | Cara Liu | 8 |
| 5 | Dev Patel | 11 | Dev Patel | 9 |
| 6 | Eli Novak | 9 | Eli Novak | 9 |
Sample data: names typed with stray spaces in A2:A6. B2 is =LEN(A2), C2 is =TRIM(A2) and D2 is =LEN(C2), each filled down. The names in A and C look alike on screen; the two LEN columns show the extra spaces.
TRIM in one formula
Say the names in column A came from a form or another system with stray spaces. In C2, type:
=TRIM(A2)and fill it down. Each name comes back with no spaces at the start or the end, and with one space between words where there were two or three. Microsoft's TRIM page says it in one line: TRIM "Removes all spaces from text except for single spaces between words." Google Sheets has the same function, and Google's TRIM help page says it "Removes leading, trailing, and repeated spaces in text."
See the spaces you cannot see
A space at the end of a cell is invisible, so check with LEN, which counts every character, spaces included. In the sample, =LEN(A3) gives 12 for Ben Okafor and =LEN(C3) gives 10 after TRIM. To count just the extra spaces in one step:
=LEN(A2)-LEN(TRIM(A2))Anything above 0 means the cell has spaces to clean. This is also why two cells that look the same can fail to match. With Ana Silva and a space after it in F2 and plain Ana Silva in F3, =F2=F3 says FALSE, and =TRIM(F2)=F3 says TRUE.
Replace the messy column with the clean one
TRIM gives you a second, clean column, and the original keeps its spaces. To swap them, select the TRIM results and copy them, then select the first cell of the original column and paste them as values: in Excel, Home > Paste > Paste Values; in Google Sheets, Edit > Paste special > Values only. Then delete the helper column.
Paste as values matters here. A plain paste would put the TRIM formulas themselves into column A, where each one would point at its own cell.
VLOOKUP or XLOOKUP says #N/A because of a space
An order code typed as B300 with a space after it does not match B300 in the price list, so =VLOOKUP(G2,D2:E4,2,FALSE) returns #N/A even though the code is right there. Clean the code inside the lookup:
=VLOOKUP(TRIM(G2),D2:E4,2,FALSE)That found B300 and returned its price, 10, in our test. XLOOKUP works the same way: =XLOOKUP(TRIM(G2),D2:D4,E2:E4). If the spaces are in the price list instead, TRIM on the code cannot help, so clean the list itself as in the section above. For codes that really are missing, the IFERROR page shows how to show a message instead of #N/A.
The space TRIM misses: character 160
Text copied from a web page, an email or a PDF often carries a non-breaking space, a different character that looks exactly like a space. Microsoft's TRIM page says it has "a decimal value of 160" and that "By itself, the TRIM function does not remove this nonbreaking space character." Google's page says the same for Sheets: "Whitespace or non-breaking space will not be trimmed." So swap each one for a normal space first, then trim:
=TRIM(SUBSTITUTE(A2,UNICHAR(160)," "))UNICHAR works in Excel 2013 and later and in Google Sheets. In Excel 2010 or older on Windows, use CHAR(160) in its place.
When TRIM has run, LEN looks right and a match still fails, this is the usual cause. A character 160 in place of a normal space does not change the length at all: Ana Silva has 9 characters either way.
Counting when some cells have spaces
COUNTIF compares whole cells, so North with a space after it is not North. In a region column where five cells say North but three of them carry a stray space, =COUNTIF(A2:A7,"North") counts 2. This counts all five:
=SUMPRODUCT(--(TRIM(A2:A7)="North"))TRIM cleans each cell in the range before the comparison, and the two minus signs turn TRUE and FALSE into 1 and 0 for SUMPRODUCT to add. If you count that column often, cleaning it once as above is the better fix.
Cells that look empty but hold spaces
A cell with only a space or two in it looks blank, but =A8="" says FALSE. This is TRUE for empty cells and for cells of spaces alike:
=LEN(TRIM(A8))=0The blank cells page puts it inside IF and has a practice tab for it.
Line breaks inside a cell
A cell where someone pressed Alt+Enter in Excel holds a line break, character 10, between the words. TRIM leaves it in place. To turn it into a space:
=TRIM(SUBSTITUTE(A2,CHAR(10)," "))Microsoft's CLEAN page says CLEAN was designed to remove "the first 32 nonprinting characters in the 7-bit ASCII code (values 0 through 31)", which takes in line breaks. But it puts nothing in their place, so Ana and Silva on two lines came out as AnaSilva in our test. Use SUBSTITUTE when the break sits between two words.
Remove every space, not only the extras
For phone numbers, postal codes or product codes where no space belongs, replace each space with nothing:
=SUBSTITUTE(A2," ","")604 555 0182 becomes 6045550182. If the text came from a web page, take out the character 160 too: =SUBSTITUTE(SUBSTITUTE(A2," ",""),UNICHAR(160),"").
Free practice sheet
Three jobs to try: clean a list of names and count how many extra spaces each one had; fix a price lookup that says #N/A for order codes typed with stray spaces; and clean names pasted from a web page, where plain TRIM still leaves four of five wrong. Yellow cells for your formulas, a Check column that says Correct or Try again, and an Answers tab. One Excel file that opens in Google Sheets too (Excel 2013 or later for the last tab). 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.
- Wedding Guest List SpreadsheetEvery guest in one place with RSVPs, meal counts and table seating.
- Inventory SpreadsheetList your items once, log stock in and out, and see what to reorder.
- Attendance TrackerA whole school year for up to 40 students with a year summary.
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.
- Count how many times each value appears in a columnA 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.
- Freeze rows and columns so the headers stay on screenMonths across the top, names down the side, and once you scroll a little both slide away and you lose track of whose row you are reading. Freezing keeps them on screen while the rest of the sheet moves.
- Highlight a whole row with a conditional formatting formulaAn order list where you want every late order to stand out, the whole row and not just the Status cell. One rule with a short formula does it, and it keeps working as statuses change.
- Create a drop-down list in Excel and Google SheetsA Status column where one person types Paid, another types payed and a third types Done is hard to count or filter. A drop-down list lets people pick from your choices instead, and it takes a minute to set up.
- Check if a cell contains a word in Excel and Google SheetsAn item list where you want every row with pen in it marked, added up or pulled out. =A2="pen" only finds a cell that is exactly pen, so the work goes to SEARCH, which looks inside the text.
- Take the first, last or middle characters from a cellA SKU like MG-012-RED packs three facts into one cell: the product type, how many come in a box and the colour. LEFT, RIGHT and MID take each piece out by its position.
- 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.







