Removing Spaces from Excel Cells
In this article
Yes, you can remove spaces from Excel cells, and you can do it quickly using the 'TRIM' function or by finding and replacing them. This is essential for cleaning data so that sorting, filtering, and mathematical functions work correctly. Before starting, ensure you have a backup of your file, as some data cleaning methods are permanent.
What You'll Need
- A computer with Microsoft Excel installed (Excel for Microsoft 365, Excel 2019, 2016, 2013, or 2010).
- A spreadsheet containing data with unwanted spaces (e.g., leading, trailing, or multiple spaces between words).
- Optional: A second, blank worksheet or a copy of your data to practice on.
Step-by-Step
- Open your Excel workbook. Launch the application and open the specific spreadsheet that contains the data with spaces you wish to remove.
- Identify the data range. Click and drag your mouse over the cells that contain the text with spaces. For example, if your data is in columns A and B from row 1 to row 1000, select the entire block A1:B1000.
- Open the 'Find and Replace' dialog box. Press the keyboard shortcut Ctrl + H. This is the universal shortcut for opening the Find and Replace window in Windows. On a Mac, the shortcut is Command + Shift + H.
- Enter the space character. In the 'Find what' field, type a single space. It is crucial that you do not press the spacebar more than once. You must type exactly one space character.
- Leave the 'Replace with' field empty. You do not need to type anything in the 'Replace with' box. This tells Excel to delete the spaces it finds.
- Choose the scope. Look at the 'Look in' dropdown menu. Ensure it is set to 'Values'. This is the most important setting. If it is set to 'Formulas', Excel will attempt to process the text as a formula, which can lead to errors. If your data is already visible values, 'Formulas' will also work, but 'Values' is the safer default for text data.
- Click 'Replace All'. Press this button to remove every instance of the space character in the selected range at once. Excel will display a popup dialog box confirming how many spaces were replaced. Click 'OK' to close this message.
- Verify the results. Scroll through your spreadsheet to ensure the data looks correct. Check a few rows of data to confirm that words are no longer separated by multiple spaces and that leading or trailing spaces have been removed.
Alternative Method: Using the TRIM Function
This method is useful if you want to keep the original data intact and create a new, cleaned column of data. It is also the best way to handle multiple spaces between words.
- Select an empty cell. Click on a cell in a column next to your data or in a blank area of the sheet where you want the cleaned data to appear.
- Enter the TRIM function. Type the following formula into the formula bar:
=TRIM(A1). Replace 'A1' with the cell reference of the first cell in your data range that contains spaces. - Copy the formula down. Click the small square in the bottom-right corner of the cell (the fill handle) and drag it down to apply the formula to the rest of your data. Alternatively, double-click the fill handle to autofill the formula down to the last row of data.
- Convert formulas to values. Since the TRIM function is a formula, the cleaned data is still technically a formula. To make it permanent, copy the column of cleaned data, right-click on the first cell of a blank column, and select 'Paste Special.' Choose 'Values' from the options and click 'OK'.
Removing Non-Printing Characters (Hidden Spaces)
Sometimes a space character does not look like a space. These are known as non-printing characters, often resulting from copying and pasting data from the web or other applications. The standard 'Find and Replace' method will not catch these invisible characters.
- Select the cells. Highlight the cells that contain the hidden spaces.
- Open the 'Find and Replace' dialog box. Press Ctrl + H.
- Click the 'More >>' button. This expands the dialog box to reveal additional options.
- Click the 'Special...' button. In the bottom-left corner of the dialog box, you will find a button labeled 'Special...'. Click it.
- Select 'Non-Printing Characters.' In the pop-up window, select the first option, which is 'Non-Printing Characters.' You will be returned to the main Find and Replace window, and the 'Find what' field will now contain a small square or a question mark.
- Leave the 'Replace with' field empty. As before, leave the replacement field blank.
- Click 'Replace All.' This will remove any non-printing characters, including tabs and other invisible whitespace, from your selected cells.
Common Mistakes
- Not setting 'Look in' to 'Values.' If you select 'Formulas' instead of 'Values' in the Find and Replace dialog, Excel may misinterpret your text as a mathematical formula, potentially deleting part of your data or resulting in a '#NAME?' error.
- Using the standard 'Find and Replace' for hidden spaces. If you are trying to remove invisible characters (non-printing characters), typing a space into the 'Find what' field will not work. You must use the 'Special' button method described above.
- Forgetting to paste as values. When using the TRIM function, the cleaned data is still a formula. If you sort your data or move it to another location, the formulas may not follow, resulting in blank cells. Always paste as values to make the change permanent.
- Replacing the wrong character. In the Find and Replace dialog, it is easy to accidentally type two spaces or a special symbol instead of a single space. Always double-check the 'Find what' field before clicking 'Replace All.'
Tips
- Use the TRIM function for data cleaning. If you are preparing a dataset for analysis or importing into another system, the TRIM function is generally superior to Find and Replace. It handles multiple spaces between words gracefully, whereas Find and Replace only removes single instances.
- Check for tabs. If your data is coming from a web source, it may contain tabs instead of spaces. You can find and replace tabs by clicking the 'Special...' button in the Find and Replace dialog and selecting 'Tab Stop.'
- Use Flash Fill. For very simple cases where you just want to remove spaces from a single column, Flash Fill is a quick alternative to formulas. Type the desired result for the first cell (e.g., if the input is 'Apple Juice', type 'AppleJuice'). Press Enter. In the next cell, type the desired result for a different item (e.g., 'Orange Soda'). Excel will often automatically detect the pattern and fill in the rest of the column. You can then press Ctrl + E to apply Flash Fill to the entire column.
- Use a helper column. If you need to keep your original data for reference, always create a helper column. Perform your data cleaning in this new column and then copy the results back to the original column using 'Paste Special' > 'Values.'
- Sort before cleaning. Sorting your data alphabetically can sometimes make it easier to spot errors, such as leading or trailing spaces that have caused the data to be misaligned. For example, 'apple' and ' apple' will sort differently, allowing you to quickly identify the problematic rows.
Troubleshooting
If you find that the spaces are still present after following the steps above, or if the data has been corrupted, try these troubleshooting steps in order.
- Check for merged cells. If your selected range contains merged cells, the Find and Replace operation may not work as expected. Unmerge the cells and try the operation again.
- Ensure you are in 'Edit Mode.' Sometimes, Excel is in a mode where you cannot edit the cells. Press F2 to enter edit mode for a specific cell, or press Esc to exit any current mode before attempting to use Find and Replace.
- Restart Excel. Closing and reopening the Excel application can resolve minor glitches that may prevent the Find and Replace function from working correctly.
- Check for add-ins. Sometimes third-party Excel add-ins can interfere with standard functions like Find and Replace. Try disabling any recently installed add-ins to see if that resolves the issue.
- Check for data validation. If your cells have data validation rules (e.g., restricting input to numbers only), the Find and Replace function may be disabled. Check the 'Data' tab for 'Data Validation' settings and ensure they are not preventing you from editing the cells.
- Try a different workbook. To isolate the issue, try performing the same Find and Replace operation on a new, blank workbook. If it works there but not in your original file, the issue may be with the specific data in your original file, such as corrupted formatting or hidden characters that are not standard spaces.
Frequently Asked Questions
Will removing spaces from a cell break a formula?
Generally, no. Removing spaces from a cell that is referenced by a formula will not break the formula. The formula will simply look at the cleaned data. However, if you remove spaces from a cell that contains a text-based formula (like a concatenation formula), it will change the result of that formula.
Can I remove spaces from an entire column at once?
Yes. You can select the entire column by clicking the column letter at the top of the sheet (e.g., click 'A' to select column A) and then use the Find and Replace method or the TRIM function to clean the entire column at once.
What is the difference between a space and a non-printing character?
A standard space is a visible character that takes up space in a cell. A non-printing character is invisible to the human eye but still takes up space in the cell's data. It often looks like a small square or a dot when you click on the cell and move the cursor next to the text. These can cause issues with sorting and filtering.
How do I remove spaces from an entire column using the TRIM function?
To remove spaces from an entire column using the TRIM function, enter the formula =TRIM(A1) in the first cell of a blank column. Then, double-click the fill handle (the small square in the bottom-right corner of the cell) to copy the formula down to the last row of data in the column.
Can I undo a 'Replace All' operation?
Yes. Excel keeps a history of your recent actions. You can press Ctrl + Z immediately after performing a 'Replace All' to undo the entire operation and restore the original data.
Is there a limit to the number of spaces I can remove?
There is no hard limit to the number of spaces you can remove. The Find and Replace function and the TRIM function are designed to handle large datasets with thousands or even millions of cells efficiently.