Replacing the Space between the Last Two Words of Each Paragraph

by Allen Wyatt
(last updated October 27, 2018)

Lisa needs a way to find the last two words in all the paragraphs of a document and replace the space between those words with a non-breaking space. She wonders if this is possible using Find and Replace.

The easiest way to do this without using a macro is to rely on the wildcard capabilities of Find and Replace. The key to doing this is to remember that you don't really need to identify the last two words in what you find—you only need to identify the last word, with its preceding space. Follow these steps:

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

  4. Select the Use Wildcards check box.
  5. In the Find What box, enter this: ( )([0-9A-Za-z~-]{1,})([.\!\?\:])(^13)
  6. In the Replace With box, enter this: ^s\2\3\4
  7. Click Replace All.

How this actually works may take just a bit of explaining. The trick here is what you are searching for, specified in step 4. Each element being searched for is surrounded by (parentheses), so it is easy to pick them out and to refer to them in the Replace With box (step 5). The four elements in the Find What box (again, surrounded by parentheses) are as follow:

  1. ( )
  2. ([0-9A-Za-z~-]{1,})
  3. ([.\!\?\:])
  4. (^13)

The first element is a single space. The second is any number of digits, letters, or dashes. (It is the last part of this element—{1,}—that specifies there can be any number of digits, letters, or dashes.) This is the element that specifies the word at the end of the paragraph, and it includes the possibility of the word containing dashes so that compound words are treated as a single word. The third element is any of the punctuation marks shown, and the fourth element is the actual end-of-paragraph marker.

The Replace With specification (step 5) simply replaces the first element (the space) with a non-breaking space, and then puts the second, third, and fourth elements back into place.

It should be noted that there are a couple of scenarios in which this Find and Replace approach will not work. First, it won't work if your paragraph has trailing spaces, between the punctuation mark and the end-of-paragraph marker. You can use Find and Replace to get rid of these throughout your document before doing the above Find and Replace.

The second situation where it won't work is if your paragraph ends with compound punctuation, such as a period followed by a quote mark or a quote mark followed by a colon. The wildcard search expects there to be only a single punctuation mark at the end of the paragraph, and it must be one of the four punctuation marks specified.

The third situation where it won't work is if there is some sort of punctuation in the final word of the paragraph. For instance, if the final word is actually a number such as 1,234 then the inclusion of the comma will stop the match from occurring. This could be dealt with by including the comma in the second element being searched for, but the number of times this might happen seemed miniscule enough that I didn't allow for it in the steps above.

If you prefer a macro-based approach to the substitution, you might consider using the following macro:

Sub LastTwo()
    Dim p As Paragraph
    Dim J As Integer
    Dim K As Integer
    Dim sTemp As String

    For Each p In ActiveDocument.Paragraphs
        J = p.Range.Words.Count
        For K = J To 1 Step -1
            sTemp = p.Range.Words(K)
            If Right(sTemp, 1) = " " Then
                p.Range.Words(K) = RTrim(sTemp) & Chr(160)
                K = 1
            End If
        Next K
    Next p
End Sub

The macro steps through each paragraph of your document and then steps backwards through the words in the paragraphs. It looks for the first word that contains a trailing space, and then replaces that space with a non-breaking space.

There is one interesting thing about this macro—it may not work as expected on all systems. If, after running it, you don't get a non-breaking space but instead see a small "cross" between the last two words, then simply replace Chr(160) in the code with Chr(202). It would be much easier if Word included a defined enumeration for a non-breaking space, but it doesn't. Instead, you need to rely on the ASCII code for a non-breaking space, which doesn't appear to be the same on all systems.


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

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (13577) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, and Word in Office 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. ...


Ignoring Accented Characters in Searches

When writing in non-English languages, there can be many variations of accented characters that are used in a word. You ...

Discover More

Refreshing the Discussion

To keep up with the Discussion, you will need to refresh the comments to make sure you are in-the-know.

Discover More

Protecting Parts of a Document

Word doesn't require you to protect entire documents. Instead, you can protect different sections within a document, as ...

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)

Using Find and Replace to Change Text Case

Can you really use Find and Replace to change the case of text in your document? Not really, but that shouldn't stop you ...

Discover More

Replacing and Renumbering

Word has a powerful find and replace capability that can help you perform quite a few changes to your documents. This tip ...

Discover More

When Replace Doesn't Work

Find and Replace is a great tool, but what are you to do if your find or replace doesn't work as you expect? This tip ...

Discover More

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

View most recent newsletter.


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 five minus 0?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)

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

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.