Removing Spaces around Dashes

Written by Allen Wyatt (last updated June 14, 2025)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Excel in Microsoft 365


1

Gilbert needs to process workbooks created by various people in his company. His biggest task seems to be removing any spaces surrounding a dash within text. Some people put spaces before the dash, some after, and some on both sides. Kenton wonders if there is an easy way to remove these spaces.

There are several ways this can be accomplished. First, of course, you could use Find and Replace to remove the spaces. Just do individual searches for a space before a dash and then a search for a space after a dash. In both cases, replace what is found with a single dash.

If you don't mind using a helper column, and the cells should not contain any spaces, then you can use the following:

=SUBSTITUTE(A1," ","")

If there are other spaces allowed in the cell, then you need to make sure that you only check for spaces around the dashes. Here's a longer formula that will do the trick:

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

Perhaps the coolest helper-column approach, though, is available for those using Excel in Microsoft 365:

=REGEXREPLACE(A1,"\s*-\s*","-")

The benefit of this formula is that it removes multiple spaces before and after dashes. Plus, it will remove non-breaking spaces, which is something the previous formulas will not do. No other spaces in the original will be affected.

Finally, if you have to do this type of replacement quite a bit, you could use a macro to do it. Here's an example of one:

Sub RemoveSpacesAroundDashes()
    Dim cell As Range
    Dim txt As String

    For Each cell In Selection
        If Not IsEmpty(cell) And VarType(cell.Value) = vbString Then
            txt = cell.Value

            ' Replace non-breaking spaces with regular spaces
            txt = Replace(txt, Chr(160), " ")
            ' Remove duplicate spaces
            txt = WorksheetFunction.Trim(txt)
            ' Remove spaces before and after dashes
            txt = Replace(txt, " - ", "-")
            txt = Replace(txt, " -", "-")
            txt = Replace(txt, "- ", "-")
            cell.Value = txt
        End If
    Next cell
    MsgBox "Spaces around dashes removed.", vbInformation
End Sub

Select the cells you want to affect and then run the macro. It removes non-breaking spaces and duplicate spaces. It then removes the spaces around any dashes.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11779) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Excel in Microsoft 365.

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

Strikethrough Shortcut Key

One common type of formatting is strikethrough, which is normally applied from the Font dialog box. There is no built-in ...

Discover More

Erasing Table Lines

When creating tables, Word provides a handy tool that you can use. Once the table is in place, you can use the table ...

Discover More

X-Axis Dates Lose Formatting

Excel makes it easy to copy charts from one workbook to another. Even so, copying may produce some surprising results for ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 365 applications with VBA programming. Written in clear terms and understandable language, the book includes systematic tutorials and contains both intermediate and advanced content for experienced VB developers. Designed to be comprehensive, the book addresses not just one Office application, but the entire Office suite. Check out Mastering VBA for Microsoft Office 365 today!

More ExcelTips (ribbon)

Using the Fill Handle from the Keyboard

The fill handle can save a huge amount of time when you are editing a worksheet. If you are really good at using the ...

Discover More

Easily Dividing Values by 1000

Sometimes the data in a worksheet isn't in the exact format desired. If you want to divide your values by 1,000, there ...

Discover More

Stopping the Deletion of Cells

You can delete cells from a worksheet, and Excel will move the remaining cells either to the left or upwards. Deletions, ...

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}] (all 7 characters, in the sequence shown) 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 two more than 8?

2025-06-15 12:09:26

J. Woolley

The Tip's 2nd formula has one more SUBSTITUTE than necessary; here's a modified version:
    =SUBSTITUTE(SUBSTITUTE(A1," -","-"),"- ","-")
But this version is better in case there are unnecessary leading, trailing, or duplicate space characters:
    =SUBSTITUTE(SUBSTITUTE(TRIM(A1)," -","-"),"- ","-")
The Tip's macro will replace a formula that returns a text result with a constant value; it should skip formula cells. Also, it only requires two Replace statements (not three) to deal with hyphens (dashes). Therefore, consider replacing the following statement
        If Not IsEmpty(cell) And VarType(cell.Value) = vbString Then
with this statement
        If VarType(cell.Value) = vbString And Not cell.HasFormula Then
Notice an empty cell will have VarType(cell.Value) = vbEmpty, not vbString. Also, consider deleting this unnecessary statement (the 1st of 3 similar statements)
            txt = Replace(txt, " - ", "-")
My Excel Toolbox includes the RegExSubstitute function described in my comments here: https://excelribbon.tips.net/T009392
If you don't have Excel 365, RegExSubstitute can be used instead of REGEXREPLACE.
Here's an alternate version of the Tip's macro that uses the Microsoft VBScript Regular Expressions library (which does not require Excel 365):

Sub RemoveSpacesAroundDashes2()
'requires VBE > Tools > References > Microsoft VBScript Regular Exp...
    Dim cell As Range '.Value is the default property of a Range object
    On Error GoTo Done 'Selection might not be appropriate
    Intersect(Selection, Selection.Worksheet.UsedRange).Select
    If Selection.Cells.Count > 1 Then
        Selection.SpecialCells(xlCellTypeConstants, xlTextValues).Select
    ElseIf VarType(Selection) <> vbString Or Selection.HasFormula Then
        GoTo Done
    End If
    With New RegExp
        .Global = True
        .Pattern = "\s*-\s*"
        For Each cell In Selection
            cell = .Replace(cell, "-")
        Next cell
    End With
Done: Beep
End Sub

Notice this RegEx macro only trims leading or trailing spaces that surround a hyphen (dash); ditto the Tip's REGEXREPLACE formula.


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.