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

Automatically Updating Fields and Links

You can update fields and links automatically when you print your document, but what if you want them updated when you ...

Discover More

Deleting Worksheets in a Macro

Processing workbooks using a macro often involves the possible creation and subsequent deletion of worksheets. When it ...

Discover More

Deleting All Tab Stops

Tab stops can be helpful when you want to align text within a paragraph. However, you might also want an easy way to get ...

Discover More

Discover the Power of Microsoft Office This beginner-friendly guide reveals the expert tips and strategies you need to skyrocket your productivity and use Office 365 like a pro. Mastering software like Word, Excel, and PowerPoint is essential to be more efficient and advance your career. Simple lessons guide you through every step, providing the knowledge you need to get started. Check out Microsoft Office 365 For Beginners today!

More WordTips (ribbon)

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

Determining if Caps Lock is On

If your macro needs to determine the status of the Caps Lock key, you need the code in this tip. Just use the Information ...

Discover More

Sentences Containing a Search Term

You can use Find and Replace to easily locate words or phrases in a document. If you want to compile a list of sentences ...

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 7 + 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.