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.

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:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

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.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Conditionally Formatting Non-Integers

The conditional formatting capabilities of Excel are very helpful when you want to call attention to different values ...

Discover More

Preventing the Insertion of Non-Breaking Spaces

When you copy and paste information from the Internet, you may end up with a lot of non-breaking spaces in the pasted ...

Discover More

Creating a Document Font List

If 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 More

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!

More WordTips (ribbon)

Understanding the For ... Next Structure

Spend any time creating Word macros, and sooner or later you will need to repeat some of your programming code a certain ...

Discover More

Automating a Two-Column Section

If you need to routinely apply two-column formatting in the middle of a document, you can automate the process by using a ...

Discover More

Deriving an Absolute Value

Want to know the absolute value of a number? It's easy to derive in VBA by using the Abs function.

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is three less than 3?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

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.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.