Written by Allen Wyatt (last updated February 20, 2023)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365
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:
Figure 1. The Replace tab of the Find and Replace dialog box.
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:
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.
Note:
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 Microsoft 365.
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!
The Find and Replace tool can get rid of trailing spaces in paragraphs quite nicely. If those spaces are at the end of ...
Discover MoreWhen searching for text, Word can helpfully highlight all instances of what is found. If you want that highlighting to be ...
Discover MoreWord allows you to use its searching capabilities to easily find multiple items in a document. What if you want to copy ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
2020-05-26 12:57:43
Andrew
Karen, you don't even need wildcards for this one. You can just use regular search and replace:
1. Search for four digits followed by a space (the search text would be, without the quotes, "^#^#^#^# "), and replace with the matched text followed by a tab (the replacement text would be, again, without the quotes, "^&^t"). In this case, ^# is the "Any Digit" code you get by clicking More->Special in the Search and Replace dialog, ^t is the "Tab Character," and ^& is the "Find What Text" (basically, the original text that the search matched).
2. Then replace the space followed by a tab (using " ^t" as the search text) with just the tab (using "^t" as the replacement text).
3. If there are years also referenced in the text, it's better to do the original search preceded by a paragraph marker -- but this may miss the first item if it's on the first line of your document or selection (using "^p^#^#^#^# ").
Andy.
2020-05-23 07:31:05
Karen
This is a great site. I assume that some variation in the code given above will solve my particular problem: I need to split the years in a chronology from the text. Replacing the first space with a tab would do the trick. This is so we can add an additional column with metadata that will let the user sort the chronology in different ways. Any advice would be most welcome, of course, but this is mainly to say thanks.
1865 The states of Connecticut, Wisconsin, and Minnesota deny African Americans the right to vote.
1865 The first African American to gain the rank of major in the U.S. Army is Martin R. Delany. Delany has many skills: he is a writer, had graduated from Howard University Medical School, and serves in the U.S. Army Medical Corps.
1865 The first African-American attorney to practice before the U.S. Supreme Court is John Rock.
1866 Fisk University, a university for African Americans, is established in Nashville, Tennessee.
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.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments