Last updated on November 9th, 2026 at 12:59 pm
To remove a space before text in Excel, type =TRIM(A2) in an empty column next to your data, fill it down, then copy the results and paste them over the original cells with Paste Special > Values. TRIM deletes spaces at the start and end of the text and shrinks any run of spaces between words to a single space. For spaces between words and non-breaking spaces as well, see how to remove extra spaces in Excel.
Applies to: Excel for Microsoft 365 (Windows and Mac), Excel 2024, 2021, 2019 and 2016, and Excel for the web. The REGEXREPLACE formula below needs Excel for Microsoft 365, and macros don’t run in Excel for the web.
If TRIM leaves the gap in place, the character usually isn’t a normal space. Text copied from web pages often starts with a non-breaking space, which needs =TRIM(SUBSTITUTE(A2,CHAR(160)," ")) instead. The sections below show how to tell which kind you have and which fix to use.
Why a leading space causes trouble
A space at the start of a cell is invisible on the grid, but Excel treats ” Alex” and “Alex” as different values. That leads to problems that look unrelated at first:
- XLOOKUP, VLOOKUP and MATCH return #N/A even though the name is clearly in the list.
- Sorting puts the padded entries at the top, away from where they belong alphabetically.
- A filter list shows what looks like the same item twice.
- Numbers with a space in front are stored as text, so SUM ignores them.
The spaces usually arrive with copied or imported data: web pages, PDFs, emails and exports from other systems.
Check what kind of space you have
Pick one problem cell (A2 in these examples) and test it before choosing a method. Type each formula in an empty cell:

=LEN(A2)and=LEN(TRIM(A2)): if the first number is bigger, TRIM will fix the cell.=CODE(A2): returns the code of the first character. 32 is a normal space, 160 is a non-breaking space, and 10 is a line break.
Here is what those checks return for some sample data:
| Cell contents | LEN | LEN(TRIM) | CODE | What it means |
|---|---|---|---|---|
| ” Invoice 1042″ (three spaces) | 15 | 12 | 32 | Normal spaces: use TRIM |
| Non-breaking space + “Alex Rivera” | 12 | 12 | 160 | TRIM does nothing: use SUBSTITUTE first |
| Line break + ” Order 77″ | 11 | 10 | 10 | Hidden character: add CLEAN |
If the cell looks indented but the formula bar shows no gap and LEN matches the visible text, there is no space at all. The cell has indent formatting; select it and choose Home > Decrease Indent.
Method 1: Remove the spaces with TRIM
This is the right fix for most sheets. Microsoft describes the TRIM function as removing all spaces from text except single spaces between words.
- Insert an empty helper column next to your data. If the text is in column A, right-click the column B header and select Insert.
- Click B2, type
=TRIM(A2)and press Enter. B2 shows the text without the leading space. - Double-click the fill handle (the small square at the bottom-right corner of B2). Excel copies the formula down to the last row of your data.
- With the results in column B still selected, press Ctrl + C (Command + C on a Mac).
- Right-click A2 and choose Paste Special > Values (the icon with “123”). Column A now holds plain cleaned text, not formulas.

- Delete column B.
Skipping step 5 is a common mistake. If you delete column A instead, every TRIM formula in column B turns into a #REF! error, because the cells they pointed to are gone.
| Original (A) | =TRIM(A2) |
|---|---|
| ” Invoice 1042″ | “Invoice 1042” |
| “Jordan Lee “ | “Jordan Lee” |
| ” Priya Shah” | “Priya Shah” |
In Excel for Microsoft 365 you can clean a whole block at once: =TRIM(A2:D100) spills a cleaned copy of the range. It needs empty cells below and to the right, or it shows #SPILL!.
Will these methods work in Excel for the web or on a Mac?
TRIM, SUBSTITUTE, CLEAN, Flash Fill and Paste Special work in Excel for the web and Excel for Mac. Use Command in place of Ctrl on a Mac. Macros don’t run in Excel for the web.
Method 2: Remove only the spaces at the start
TRIM also changes spacing between words, as “Priya Shah” shows above. If that spacing must stay exactly as it is (fixed-width codes, addresses with deliberate gaps), strip only the leading spaces.
In Excel for Microsoft 365, use REGEXREPLACE:
=REGEXREPLACE(A2,"^\s+","")
The pattern ^\s+ means “one or more space-like characters at the very start.” It removes leading spaces, tabs and line breaks and leaves “Priya Shah” with its double space. To strip only trailing spaces, use "\s+$" as the pattern instead.
In any version of Excel, this formula finds the first character that isn’t a space and returns everything from that point on:
=MID(A2,FIND(LEFT(TRIM(A2),1),A2),LEN(A2))
Both formulas return text, so paste the results back as values as in Method 1. Neither one handles a non-breaking space by itself; see the next method.
Microsoft’s TRIM documentation notes that TRIM was built for the standard space (character 32) and doesn’t remove the non-breaking space (character 160). That is why TRIM sometimes appears to do nothing on data pasted from a web page.
Turn the non-breaking spaces into normal spaces first, then trim:
=TRIM(SUBSTITUTE(A2,CHAR(160)," "))
If =CODE(A2) returned 10, 13 or 9 (line break, carriage return or tab), add CLEAN, which strips nonprinting characters with codes 0 to 31:
=TRIM(CLEAN(SUBSTITUTE(A2,CHAR(160)," ")))
This one formula covers all three rows of the sample table: it returns “Invoice 1042”, “Alex Rivera” and “Order 77”. CLEAN alone doesn’t remove character 160, so the SUBSTITUTE part is still needed. Microsoft’s guide to cleaning data in Excel recommends the same TRIM, CLEAN and SUBSTITUTE combination.
If you want to keep double spaces between words, combine the same idea with Method 2: =REGEXREPLACE(SUBSTITUTE(A2,CHAR(160)," "),"^\s+","").
How do I remove letters or symbols as well as spaces?
SUBSTITUTE can swap out any character, not only spaces. For more patterns, such as removing the first few characters, see removing characters from a cell in Excel.
Once the spaces are gone, it’s worth checking the same sheet for duplicates, inconsistent capitalization and numbers stored as text. The checklist in cleaning data in Excel walks through those next steps.
Method 4: Let Flash Fill copy your example
Flash Fill works without formulas. It watches what you type and repeats the pattern down the column.
- In the empty cell next to your first entry, type the text exactly as it should look, without the leading space.
- Press Enter to move to the next row.
- Press Ctrl + E, or select Data > Flash Fill. Excel fills the rest of the column.
- Scan the results, especially rows that look different from the first one. If a row is wrong, fix it by hand.
Flash Fill writes plain values, so there’s nothing to paste over. Copy the column over the original, or keep it and delete the old one. It does best with consistent data; on mixed lists, check it carefully. The same tool can also split names and codes apart, as covered in splitting columns with Text to Columns and Flash Fill.
Why does my data still sort strangely after trimming?
Check that you pasted values over the original column. If you sorted a range that still contains the old text, or the cleaned numbers are still stored as text, the order will look wrong. Convert number-like text with VALUE before sorting.
Method 5: Clean imports automatically with Power Query
If you paste or import the same kind of report every week, Power Query can remove the spaces each time you refresh.
- Click any cell in your data and select Data > From Table/Range. If Excel asks, confirm that your data has headers. The Power Query Editor opens.
- Click the column header you want to clean. Hold Ctrl to select more than one column.
- Select Transform > Format > Trim. The leading and trailing spaces disappear in the preview.

- For line breaks and other hidden characters, also select Transform > Format > Clean.
- Select Home > Close & Load. Excel puts the cleaned data on a new sheet.
When the source data changes, select Data > Refresh All and the same steps run again. Power Query’s Trim works differently from the worksheet TRIM function: it removes whitespace at the start and end of each value but leaves the spaces between words untouched. Microsoft’s Text.Trim reference doesn’t list exactly which characters count as whitespace, so if a non-breaking space survives, use Transform > Replace Values to replace it with nothing before trimming. If you can’t find From Table/Range, see where Power Query lives in each Excel version.
Method 6: Clean cells in place with a macro
A macro fixes the selected cells directly, with no helper column. It’s handy if you clean similar sheets often. Excel for the web can’t run it.
- Save a copy of your workbook. Macro changes can’t be undone with Ctrl + Z.
- Open the Visual Basic editor: press Alt + F11 on Windows, or select Developer > Visual Basic. On a Mac, turn on the Developer tab first under Excel > Preferences > Ribbon & Toolbar. Then select Insert > Module.
- Paste this code into the module window:
Sub RemoveLeadingSpaces()
Dim c As Range
For Each c In Selection
If Not c.HasFormula And VarType(c.Value) = vbString Then
c.Value = LTrim(Replace(c.Value, Chr(160), " "))
End If
Next c
End Sub
- Close the editor, select the cells to clean, press Alt + F8 (or select Developer > Macros), choose RemoveLeadingSpaces and select Run.
LTrim removes only leading spaces, so spacing between words stays as it was. The code first swaps non-breaking spaces for normal ones and skips cells that contain formulas. Note that this swap affects non-breaking spaces anywhere in the cell, not only at the start. To keep the macro, save the file as an Excel Macro-Enabled Workbook (.xlsm).
When Find and Replace is the better tool
Find and Replace can’t target only the space at the start of a cell. If you press Ctrl + H, type one space in Find what, leave Replace with empty and select Replace All, Excel removes every space in the selection, so “Jordan Lee” becomes “JordanLee”.
That’s exactly right for data that should never contain spaces, such as product codes, phone numbers or account IDs. Select only that column first, because Replace All works on the whole sheet when a single cell is selected. To remove non-breaking spaces this way, click in Find what and type Alt + 0160 on the numeric keypad, or copy the character from an affected cell and paste it into the box.
Turn cleaned numbers back into numbers
TRIM, SUBSTITUTE, CLEAN and REGEXREPLACE always return text, even when the result looks like a number. A cleaned “1250” that sits on the left side of the cell won’t be counted by SUM. Wrap the formula in VALUE:
=VALUE(TRIM(A2))
Numbers copied from other systems sometimes use spaces as thousands separators, such as “1 250 000”. TRIM can’t help, because those spaces are between digits. Remove every space and convert:
=VALUE(SUBSTITUTE(A2," ","")) returns 1250000.
If that gives #VALUE!, the separators are non-breaking spaces. Use =VALUE(SUBSTITUTE(A2,CHAR(160),"")) instead. For number columns where the leading zeros matter, such as ZIP codes or part numbers, keep the result as text and see keeping leading zeros in Excel instead of using VALUE.
Fix lookups without changing the data
If you can’t edit the source sheet, trim inside the lookup. In Excel for Microsoft 365 and Excel 2021 or later:
=XLOOKUP(TRIM(E2),TRIM(A2:A100),B2:B100)
This trims both the value you’re searching for and the list you’re searching in. It recalculates the trimming every time the sheet changes, so on large sheets it’s faster to clean the data once with Method 1. More lookup examples are in this XLOOKUP walkthrough.
TRIM vs. TRIMRANGE
The newer TRIMRANGE function in Excel for Microsoft 365 sounds related, but it doesn’t touch the characters inside cells. It trims empty rows and columns from the edges of a range so formulas only use the filled part. For spaces before text, stick with TRIM. If you’re curious what TRIMRANGE is for, see using TRIMRANGE for dynamic ranges.
Stop new leading spaces from being typed
On a shared sheet, Data Validation can refuse entries that have extra spaces.
- Select the input cells, for example A2:A500.
- Select Data > Data Validation.
- On the Settings tab, set Allow to Custom.
- In Formula, type
=A2=TRIM(A2), using the first cell of your selection. - On the Error Alert tab, type a message such as “Please remove extra spaces,” then select OK.
Excel now rejects typed entries with leading, trailing or doubled spaces. Validation doesn’t check data that was already in the cells or data pasted over them, and a leading non-breaking space passes this test. To allow double spaces between words and block only leading spaces, use =LEFT(A2,1)<>" " as the formula.
If the space still won’t go away
- The formula shows as text instead of a result. The helper column is formatted as Text. Change it to General on the Home tab, then click in the cell, press F2 and Enter.
- The results don’t update. Check Formulas > Calculation Options and make sure Automatic is selected.
- TRIM worked on some rows but not others. Run the CODE check on one of the stubborn rows; it’s usually character 160 or a line break (Method 3).
- Flash Fill didn’t start. Type two examples instead of one, then press Ctrl + E again. On Windows, also confirm that Automatically Flash Fill is checked under File > Options > Advanced, then restart Excel.
- #SPILL! after a range formula. Something is blocking the cells the results need. Clear them, or use a single-cell formula and fill it down.
Confirm the fix and undo it if needed
To confirm, put =SUMPRODUCT(--(LEN(A2:A100)<>LEN(TRIM(A2:A100)))) in an empty cell. It counts how many cells in A2:A100 still have extra normal spaces; 0 means they’re all clean. It doesn’t count cells that start with a non-breaking space, so run the CODE check on a sample too. A lookup that returned #N/A before should now return a value.
Undo depends on the method. Paste Special, Flash Fill and Find and Replace can be reversed with Ctrl + Z right away. Power Query never changes your original range, so you can delete its output sheet. A macro can’t be undone, which is why the macro steps start with saving a copy.

Matt Jacobs writes and maintains the guides on Support Your Tech, which he started in 2017. He spent most of his career keeping small-business PCs running and still takes the occasional consulting job, so the steps here come from fixing the same problems on other people’s computers. Every guide is checked on his own Windows 11, Windows 10, Mac, iPhone and iPad before it goes up.