Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Word in Microsoft 365. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Occurrences of a Text String within a Document.
Written by Allen Wyatt (last updated October 11, 2025)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Word in Microsoft 365
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.
This can be done within a macro by using Find and Replace, similar to how you could use Find and Replace manually to determine a count. Each iteration of Find and Replace can increment a variable to derive the desired count. The following is an example of how this technique could work:
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 = .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 "There were " & Replacements & " instances of " _
              "" & .Text & " in the document."
        Else
            MsgBox .Text & " was not found in the document/selection."
        End If
    End With
End Sub
If desired, you can make a text selection before running the macro. In that that way, the count will only apply to the selected text. If you want to provide a count for the entire document, then you should not make a text selection. The macro prompts the user for the search text, and then also uses that as the replacement text. It then does successive searches to increment the Replacements variable. Finally, a message box displays how many replacements were made, which is the count that Marc desires.
Before running the macro, make sure you save your document. It does change document content, so you will want to ensure that you don't save your document after running the macro.
You should also be aware that using a macro approach such as this does have a drawback—it searches only in the document body, and not in any other stories used by Word. This means that it won't search in areas such as footnotes, endnotes, headers, footers, or text boxes. There are actually 17 different types of stories that can be included in a Word document; you can find more about them here:
https://learn.microsoft.com/en-us/office/vba/api/word.wdstorytype
A manual Find and Replace (which Marc doesn't want to do) will look through most all of the stories in coming up with its count. In VBA (which Marc wants to use), a Find and Replace will only go through the current story. Expanding the macro to search all stories would require a significant reworking of this simple macro.
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 (11941) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Word in Microsoft 365. You can find a version of this tip for the older menu interface of Word here: Occurrences of a Text String within a Document.
 
                        The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2019. Spend more time working and less time trying to figure it all out! Check out Word 2019 For Dummies today!
Want a fast way to add brackets around a selected word? You can use this simple macro to add both brackets in a single step.
Discover MoreWord has a powerful Find and Replace capability. If you want to change the case of what is found, however, then Find and ...
Discover MoreWord allows you to create a macro that is run automatically whenever the program is started. If you want to bypass the ...
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 © 2025 Sharon Parq Associates, Inc.
Comments