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.
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!
When you receive documents created by others, it is not unusual that you'll want to edit what they've written. For ...
Discover MoreIf you use the highlighter tool extensively, you may want to search for text that is highlighted using a particular ...
Discover MoreThe Find and Replace tool is designed to help you find and replace information as quickly as possible. However, you may ...
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