3 Ways to Replace Spaces Between Characters in Excel Text Strings
by Avantix Learning Team | Updated October 20, 2021
Applies to: Microsoft® Excel® 2013, 2016, 2019, 2021 and 365 (Windows)
In Excel, you can replace spaces in text strings with underscores, dashes or other values using Find and Replace, by creating a formula or by using Flash Fill.
Recommended article: How to Delete Blank Rows in Excel (5 Fast Ways to Remove Empty Rows)
Do you want to learn more about Excel? Check out our virtual classroom or in-person Excel courses >
1. Replacing spaces using Find and Replace
To replace spaces using Find and Replace:
- Select the range of cells containing text strings that include spaces you want to replace.
- Press Ctrl + H to display the Find and Replace dialog box. You can also click the Home tab in the Ribbon and select Replace in the Find & Select group.
- In the Find what box, type a space.
- In the Replace with box, type an underscore, dash, or other value. If you want to replace the space with nothing, leave the box blank.
- Click Replace All. A dialog box appears indicating the number of replacements.
- Click OK.
Below is the Find and Replace dialog box in Excel to replace spaces with underscores:
2. Replacing spaces using a formula
You can also create a formula and use the SUBSTITUTE function to replace spaces in text strings.
The Excel syntax for the SUBSTITUTE function is:
=SUBSTITUTE(text, old_text, new_text, [instance_number])
The text is typically a reference to a cell. The old text is the text you want to replace. The new text is the text you use as a replacement. Instance number is optional.
The simple syntax of the SUBSTITUTE function is:
=SUBSTITUTE(cell containing the data you want to replace, the value you want to remove, the value you want to use as a replacement, instance if required)
Text must appear in quotation marks or double quotes.
For example, if you want to replace spaces with underscores in cell B1 based on a text string in A1, the formula would be:
If you need to remove leading and trailing spaces, you can combine the SUBTITUTE function with the TRIM function as follows:
To convert the formula to text, simply copy and paste the formula as values.
3. Replacing spaces using Flash Fill
If you have Excel 2013 or later, you can also try using Flash Fill to replace spaces. Because Flash Fill needs to see a pattern to the left, you will likely need to enter several examples before running Flash Fill.
To change spaces to underscores or other values:
- Click in a cell to the right of the cell with the spaces you want to replace.
- Enter the same entry as the original cell with underscores instead of spaces and press Enter.
- Repeat for the next 3 or 4 cells below the first cell.
- In the cell below the last entry, press Ctrl + E to run Flash Fill.
Be sure to check the entries generated by Flash Fill.
Did you find this article helpful? If you would like to receive new articles, JOIN our email list.
Our instructor-led courses are delivered in virtual classroom format or at our downtown Toronto location at 18 King Street East, Suite 1400, Toronto, Ontario, Canada (some in-person classroom courses may also be delivered at an alternate downtown Toronto location). Contact us at firstname.lastname@example.org if you'd like to arrange custom instructor-led virtual classroom or onsite training on a date that's convenient for you.
Copyright 2023 Avantix® Learning
You may also like
Power Query in Excel is a powerful data transformation tool that allows you to import data from many different sources and then extract, clean, and transform the data. You will then be able to load the data into Excel or Power BI and perform further data analysis. With Power Query (also known as Get & Transform), you can set up a query once and then refresh it when new data is added. Power Query can import and clean millions of rows of data.
You can freeze one or more rows in an Excel worksheet using the Freeze Panes command. If you freeze rows containing headings, the headings will appear when you scroll down. You can freeze columns as well so when you scroll to the right columns will be frozen.
You can remove or hide gridlines in Excel worksheets to simplify worksheet design. By default, gridlines are displayed but do not print. Gridlines are applied to entire worksheets or workbooks, not to specific cells. If you hide gridlines on one worksheet, it doesn't affect other sheets in the same workbook.
Microsoft, the Microsoft logo, Microsoft Office and related Microsoft applications and logos are registered trademarks of Microsoft Corporation in Canada, US and other countries. All other trademarks are the property of the registered owners.
Avantix Learning |18 King Street East, Suite 1400, Toronto, Ontario, Canada M5C 1C4 | Contact us at email@example.com