Written by Allen Wyatt (last updated March 15, 2025)
This tip applies to Word 97, 2000, 2002, and 2003
Marc is looking for the fastest, most efficient way—within a macro—to determine a count of how many times a particular text string occurs within a document. Unfortunately there is no way to do this with a simple command or two; instead you need to "step through" a document using the Find and Replace feature of Word.
First, make a temporary copy of your document so that you don't run the risk of messing up your original document. Then use a variable in your macro to count the number of times the desired text gets replaced, and increment the variable every time a replacement occurs. In the following example, the number of times will end up in the variable Replacements. You can then use the value or convert the value to a string to display it.
Sub CountReplacements Dim Replacements As Integer Replacements = 0 Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find .Text = InputBox("Enter the text you want to find:") .Replacement.Text = InputBox("Enter the replacement text:") .Forward = True .Wrap = wdFindContinue .Format = False .Execute Replace:=wdReplaceOne Do Until Not .Found .Execute Replace:=wdReplaceOne Replacements = Replacements + 1 Selection.MoveRight Unit:=wdCharacter, Count:=1 Loop If Replacements <> 0 Then MsgBox _ "" & .Text & " has been replaced " & _ CStr(Replacements) & " times with " & _ .Replacement.Text Else MsgBox .Text & " was not found in the document/selection." End If End With End Sub
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 (3368) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Occurrences of a Text String within a Document.
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!
You can use a macro to print to any printer you have defined in Windows. It is good practice, if you are changing which ...
Discover MoreIf you want a list of all the fonts used in a document, the answer isn't as simple as you may think. This tip uses macros ...
Discover MoreSometimes it is helpful to know how often a particular phrase appears within a document. If you need to know such a ...
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 menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon 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 © 2025 Sharon Parq Associates, Inc.
Comments