Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, and 2016. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Using a Formula to Replace Spaces with Dashes.

Using a Formula to Replace Spaces with Dashes

by Allen Wyatt
(last updated July 22, 2017)

6

Viv has a worksheet that contains lots of product descriptions. She needs a way to replace all the spaces between words with dashes. She knows she could use Find and Replace, but would prefer to use a formula to do the replacements.

Perhaps the easiest way to accomplish this task, using a formula, is to rely on the SUBSTITUTE function. At its most simple, SUBSTITUTE is used to replace one character in a text string with a different character. Thus, assuming your original product description is in cell A1, you could use the following:

=SUBSTITUTE(A1," ","-")

This formula locates every space in the text and replaces them with dashes. If you have additional product descriptions in column A and you placed this formula in cell B1, just copy the formula down as many cells as necessary.

If you are concerned that there may be leading or trailing spaces in your data, then you can expand the formula using the TRIM function:

=SUBSTITUTE(TRIM(A1)," ","-")

Either of the formulas presented so far does great at replacing regular spaces within text. Understand, however, that if you are importing your original text from a program other than Excel, the text may contain characters that look like regular spaces, but aren't really. In that case, the above approaches won't work and you'll need to do some detective work to figure out exactly what the faux spaces really are so you can replace them.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12488) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel here: Using a Formula to Replace Spaces with Dashes.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Returning Blanks with VLOOKUP

Normally the VLOOKUP function returns a value, and if it can't return a value it returns a zero. Here's how you can use ...

Discover More

Field in Footer Won't Update

Word provides a handy shortcut that allows you to update the fields in any text you've selected. When you select your ...

Discover More

Translating Documents

Need some help getting your document translated into a different language? Docs has a built-in translation tool that can ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Summing Based on Part of a Control Cell

When analyzing data, you may have a need to calculate a sum based on just part of a particular cell. This tip examines ...

Discover More

Identifying Digit-Only Part Numbers Excluding Special Characters

When working with data in Excel, you often need to determine if that data meets criteria that you specify. This tip ...

Discover More

Extracting a State and a ZIP Code

Excel is often used to process or edit data in some way. For example, you may have a bunch of addresses from which you ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 6 - 6?

2019-06-28 10:41:20

E

BTW, final formula for this use case was:
=LOWER(SUBSTITUTE(TRIM(B2)," ","_"))

Replaced all spaces with underscore, and transformed all CHARACTERS to lowercase.
Now have well formed column headers for data munging!


2019-06-28 10:38:52

E

Super helpful, the TRIM is a nice addition as well. Many thanks!


2019-03-27 14:01:31

hafiz salahuddin

Very helpful :) thanks so much


2018-09-18 06:09:25

Robert

Thanks Allen, this is very helpful. Really appreciate the time and effort you've made to share your knowledge.


2017-07-25 03:40:16

Peter M

Spot on Alex!

I use the CellView addin from Chip Pearson (www.cpearson.com/excel/cellview.htm) to see the ascii code.

I have several macros either Trimming data or replacing spaces all of which I ultimately changed to incoropate CHR(160) as a space to avoid the hassle of things not working properly when CHR(160) is encountered.

I think there is a good case for the TRIM function automatically assuming CHR(160) is a space and processing it accordingly.


2017-07-22 08:03:41

Alex B

If you find trim isn't removing the leading & trailing spaces and / or substitute isn't handling the spaces, try isolating the character usIng MID () and finding its ASCII code using CODE() ie
=CODE(MID (A1,3,1))
- where one of the offending characters is in the 3rd position.

With a SAP application output this is likely to give you a result of 160 which the ASCII code for a non-breaking space.
You would then need to modify your substitute formula to be
=SUBSTITUTE (A1,CHAR (160),"-")

There are quite a few sources for ASCII tables but this one gives English description for the code
http://www.ascii-code.com


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.