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 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 Easy Ways with Shortcuts)

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:

  1. Select the range of cells containing text strings that include spaces you want to replace.
  2. 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.
  3. In the Find what box, type a space.
  4. 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.
  5. Click Replace All. A dialog box appears indicating the number of replacements.
  6. Click OK.

Below is the Find and Replace dialog box in Excel to replace spaces with underscores:

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 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)

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:

=SUBSTITUTE(A1,” “,”_”)

If you need to remove leading and trailing spaces, you can combine the SUBTITUTE function with the TRIM function as follows:

=SUBSTITUTE(TRIM(A1),” “,”_”)

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:

  1. Click in a cell to the right of the cell with the spaces you want to replace.
  2. Enter the same entry as the original cell with underscores instead of spaces and press Enter.
  3. Repeat for the next 3 or 4 cells below the first cell.
  4. In the cell below the last entry, press Ctrl + E to run Flash Fill.

Be sure to check the entries generated by Flash Fill.

Subscribe to get more articles like this one

Did you find this article helpful? If you would like to receive new articles, JOIN our email list.

More resources

How to Quickly Delete Blank Rows in Excel (5 Ways)

How to Use Flash Fill in Excel (4 Ways with Shortcuts)

3 Excel Strikethrough Shortcuts to Cross Out Text or Values in Cells

How to Replace Blank Cells in Excel with a Value from the Cell Above

10 Excel Flash Fill Examples (Extract, Combine, Clean and Format Data with Flash Fill)

Related courses

Microsoft Excel: Intermediate / Advanced

Microsoft Excel: Data Analysis with Functions, Dashboards and What-If Analysis Tools

Microsoft Excel: Introduction to Visual Basic for Applications (VBA)

VIEW MORE COURSES >

Our instructor-led courses are delivered in virtual classroom format or at our downtown Toronto location at 1 Yonge Street, Suite 1801 (Toronto Star Building), Toronto, Ontario, Canada (some in-person classroom courses may also be delivered at an alternate downtown Toronto location). Contact us at info@avantixlearning.ca if you'd like to arrange custom onsite training or an online course on a date that's convenient for you.

Copyright 2021 Avantix® Learning

You may also like

How to Use Conditional Formatting in Excel to Highlight Dates Before Today (3 Ways)

How to Use Conditional Formatting in Excel to Highlight Dates Before Today (3 Ways)

You can use conditional formatting in Excel to highlight cells containing dates before today or within a date range before the current date. In a worksheet, you can use conditional formatting to highlight selected cells by filling them with a color based on rules or conditions. This type of formatting is helpful if you want to highlight past due dates such as invoices that are 30, 60 or 90 days overdue.

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 | 1 Yonge Street, Suite 1801 (Toronto Star Building), Toronto, Ontario, Canada M5E 1W7 | info@avantixlearning.ca

Summary
How to Replace Spaces in Excel with Underscores (_), Dashes (-) or Other Values
Article Name
How to Replace Spaces in Excel with Underscores (_), Dashes (-) or Other Values
Description
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.
Author
Publisher Name
Avantix Learning

Pin It on Pinterest

Share This