Removing Leading Spaces in a Table

by Allen Wyatt
(last updated February 18, 2017)

3

Sometimes when Eileen converts text to a table or if she imports a table from another program, some of the cells have a leading space in them, before the first actual character. She wonders if there is a way to use Find and Replace to remove these leading spaces only in the cells of the table.

There's actually one very quick way to get rid of the leading spaces without the need to use Find and Replace. Let's assume that you want the text in the cells to be left-aligned. Follow these steps:

  1. Select the cells you want to affect in the table.
  2. Display the Home tab of the ribbon.
  3. Click the Center Text tool in the Paragraph group. All of the text in the selected cells is now centered.
  4. Click the Align Text Left tool in the Paragraph group.

That's it; when you perform step 3, the leading spaces are removed automatically by Word. Step 4 simply puts your text back to where you originally wanted it aligned. An extra bonus is that these steps actually remove any white space at the beginning of the text, not just spaces.

If you absolutely want to use Find and Replace to get rid of the spaces, you cannot do it using a single Find and Replace operation. Here's an idea of how you can do it using multiple passes:

  1. Select the table.
  2. Press Ctrl+H. Word displays the Replace tab of the Find and Replace dialog box.
  3. Click the More button if it is available. (See Figure 1.)
  4. Figure 1. The Replace tab of the Find and Replace dialog box.

  5. Erase anything in the Find What box, but make sure the insertion point is still in the box.
  6. Click the Format button and then click Style. Word displays the Find Style dialog box. (See Figure 2.)
  7. Figure 2. The Find Style dialog box.

  8. Select the style used by the text in the table. (There's a good chance this will be the Normal style.)
  9. In the Replace With box, enter $#$^&. This specifies you want to preface whatever is found (which is any text formatted with the style you chose in step 6) with a unique set of characters ($#$).
  10. Click Replace All.
  11. In the Find What box, enter $#$^w. This specifies that you want to find the unique characters followed by any white space. (This includes your leading spaces.)
  12. Click the No Formatting button.
  13. Delete anything in the Replace With box.
  14. Click Replace All.
  15. In the Find What box, enter $#$. This specifies that you want to find any remaining unique characters.
  16. Click Replace All.

You could, if desired, also use a macro to remove leading spaces. All the macro needs to do is to step through all the cells in a table and trim off any leading spaces:

Sub DeleteCellLeadingSpace()
    Dim aCell As Cell
    Dim aRow As Row
    Dim cText As String

    If Selection.Information(wdWithInTable) Then
        For Each aRow In Selection.Tables(1).Rows
            For Each aCell In aRow.Cells
                cText = aCell.Range.Text
                cText = LTrim(cText)
                aCell.Range.Text = Left(cText, Len(cText) - 2)
            Next aCell
        Next aRow
    Else
        MsgBox "Insertion point must be in a table."
    End If
End Sub

Note that when this macro stuffs information back into a cell (from the cText variable), it only stuffs back everything but the final two characters. If you don't do this, you'll end up with an additional hard return in each cell of the table.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (1263) applies to Microsoft Word 2007, 2010, 2013, and 2016.

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

Appending to a Non-Document Text File

Your macros can easily add information to the end of an existing text file. This is done by opening the target file in Append ...

Discover More

Editing the Custom Spelling Dictionaries

Excel provides spell-checking capabilities on the text you enter in a worksheet. It utilizes the same dictionaries and custom ...

Discover More

Copying Headers and Footers

Need to copy headers and footers from one worksheet to another? How about from one workbook to another? Here are some ...

Discover More

Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!

More WordTips (ribbon)

Formatting an ASCII Table with Spaces

When you get a text file from a program other than Word, tabular information may be formatted with nothing but spaces in ...

Discover More

Copying All Tables to a New Document

Need to make a copy of every single table in a document? There's a quick and easy way to do it using the macro in this tip.

Discover More

Selecting Individual Cells in a Table

Many times before applying formatting or doing another operation, you need to select an individual cell in a table. Here's ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, 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. 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 one less than 4?

2017-02-21 19:12:08

Gretchen

That center-and-left tip is AWESOME!! This will save me a LOT of time. THANK YOU!


2017-02-20 08:57:16

Bob

Centering and changing back to Left Align gets rid of the leading spaces whether in a table or not. I had a specific instance of copying addresses from our website to a doc and did not know this. Works great! Thanks!


2017-02-18 09:08:47

Joann Ingoglia

You know yr stuff!


This Site

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

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, 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.