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.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 365 applications with VBA programming. Written in clear terms and understandable language, the book includes systematic tutorials and contains both intermediate and advanced content for experienced VB developers. Designed to be comprehensive, the book addresses not just one Office application, but the entire Office suite. Check out Mastering VBA for Microsoft Office 365 today!
Writing macros often involves selecting different parts of your document so that some sort of processing can be ...
Discover MoreNeed to repeat an action a whole bunch of times? You can do it a time or two using keyboard shortcuts, but you'll need a ...
Discover MoreWhen processing a document with a macro, you may need to have the macro repaginate the text. It's easy to do using 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