Written by Allen Wyatt (last updated May 20, 2024)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021
Leo need to search for two superscripted digits followed by a period and replace it with a period followed by the superscripted digits. Only the digits are superscripted, not the period. Leo wonders if this is something that can be done with Find and Replace, or if he needs to use a macro.
The answer is "yes," but it depends on the nature of the text and what you are trying to do. (I know that answer sounds squirrelly, but stick with me here.) The nature of the problem here is that there is mixed formatting in what you are looking for. The traditional approach to this type of problem is to do multiple passes of Find and Replace to get to your result.
The fact that you are dealing with digits, however, may allow you to do the Find and Replace in a single pass, without regard to the mixed formatting. If the only time your text contains two digits followed by a period is when the two digits are superscripted, then you can use Find and Replace to do the work. Just follow these steps:
Figure 1. The Replace tab of the Find and Replace dialog box.
This replaces all occurrences of two digits followed by a period with a period followed by those same digits. If you want to replace only some of them, you can, in step 6, step through each match instead of replacing all of them. This works because you are not seeking to do any formatting change with the Find and Replace operation, so the original formatting of the characters remains the same.
If you want to replace everything and make sure that the two digits are superscripted, then you'll need to resort to a macro. The following is an example of one that should do the trick:
Sub SwapSupDigits() Selection.Find.ClearFormatting With Selection.Find .Text = "([0-9]{2})(.)" .Replacement.Text = "\2\1" .Forward = True .Wrap = wdFindAsk .Format = True .MatchCase = False .MatchWholeWord = False .MatchAllWordForms = False .MatchSoundsLike = False .MatchWildcards = True End With Do While Selection.Find.Execute With Selection .MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend If .Font.Superscript = True Then ' Found superscript numbers .Collapse Direction:=wdCollapseStart .Find.Execute Replace:=wdReplaceOne End If .Collapse Direction:=wdCollapseEnd End With Loop End Sub
The first portion of the macro sets up the search the same as the manual steps did, earlier. If a match is found, then the macro looks at the digits and swaps the position with the period only if the digits are superscripted.
I started by saying that the answer to the problem depends on the nature of the text you are working with. There is one other time when the above solutions won't be satisfactory—if the superscripted digits are actually footnote or endnote references. While these appear to be superscripted digits, they are really superscripted markers that indicate where the footnote or endnote is anchored. Treating them as regular digits (as the previous approaches do) won't result in any matches because they aren't digits.
If that is the type of text you are dealing with, then the following usage of Find and Replace should work fine:
Note, in step 4, that the first part of what you are looking for is (^2). This is a special code, surrounded by parentheses, that will match any footnote or endnote marker. The code is undocumented (or, I haven't been able to find any official documentation about it in the past two decades) and it only works if you are doing a wild card search.
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 (13918) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021.
Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!
The Find and Replace capabilities of Word are quite powerful. If you want to change the formatting of just a part of text ...
Discover MoreHidden text is a great boon if you want to make sure something doesn't show up on the screen or on a printout. If you ...
Discover MoreWhen using the Find and Replace feature of Word, you can search for more than plain text. You can also search for ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
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