Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, and 2013. 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: Counting the Instances of a Text String.

Counting the Instances of a Text String

Written by Allen Wyatt (last updated August 15, 2022)
This tip applies to Word 2007, 2010, and 2013


6

When you are working in a large document, you may want to know how many times a particular text string occurs within a portion of the document. One way you can find this information is to create a macro that will examine a selected range of text to see how many instances it contains. Consider the following macro:

Sub CountString()
    Dim MyDoc As String
    Dim txt As String
    Dim t As String

    MyDoc = ActiveDocument.Range.Text
    txt = InputBox("Text to find")
    t = Replace(MyDoc, txt, "")
    MsgBox (Len(MyDoc) - Len(t)) / Len(txt) & " occurrences of " & txt
End Sub

Select the text you want analyzed and then run the macro. It assigns the selected text to the MyDoc variable (so nothing in the actual document is affected) and then asks you to enter the text string you want to find. The Replace function is then used to replace all the instances of that string within the MyDoc variable. The text, after the replacement, is assigned to the variable t. The length of the "before" text (MyDoc) and the "after" text (t) is compared and the number of replacements calculated.

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 (5651) applies to Microsoft Word 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Word here: Counting the Instances of a Text String.

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

Margin Notes in Word

Some types of documents rely upon margin notes to the left or right of your main text. Getting these to appear in Word ...

Discover More

Breaking a Document Link

Word allows you to link external information into your documents. If you no longer need to maintain the active link, you ...

Discover More

Removing Spaces

Need to get rid of spaces in a range of cells? There are two ways you can approach the task, as described here.

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 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!

More WordTips (ribbon)

Automating a Manual Process with a Macro

The entire purpose of macros is to allow you to automate repetitive or tedious tasks with relative ease. How easy the ...

Discover More

Macro Won't Limit Replacements to a Selection

When you are using Find and Replace under macro control, you can specify what you want it to do when the end of your ...

Discover More

Displaying a Message in the Status Bar

A great place for your macro to display status information is, well, in the status bar. Displaying the information is ...

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 2 + 6?

2016-11-21 13:41:23

Matthew H

I couldn't get this to work in my version of Word. It would only give me stats for the whole document, not my selection. I am using Word 2016.

To get it to work properly, I just changed

MyDoc = ActiveDocument.Range.Text

to

MyDoc = Selection.Text


Enjoy.


2016-02-16 09:58:47

David Goldberg

So it is. :) Well, it didn't matter, because I was familiar enough with the process that I didn't have to read the third paragraph. But posterity will appreciate your correction, no doubt.


2016-02-15 23:17:41

Morris Manning

David: The third paragraph previously submmitted is partially garbled. Assign any legal key combination. (I used Alt,C,S without the punctuation.)

If you prefer to assign "CountString" to the Quick Access Toolbar, open the "Customize Quick Access Toolbar" dialog. Select Macros" from the "Choose Commands From" drop-down list. Select "CountString" and add to the QAT


2016-02-15 22:54:13

David Goldberg

Got it- worked fine. Thanks Morris!


2016-02-15 22:15:11

Morris Manning

David: Open Word and then the VB editor (Alt + F11). Copy the "CountString" macro to a module in the Normal Project. Close VB editor.

Right-click the ribbon, select "Customize the Ribbon" dialog and, in the lower left corner, open the keyboard shortcuts dialog. In the Catagories section scroll down to and select Macros. A list of macros from the Normal Project will be listed. Select 'CountString' from the list.

Enter a key combination in "Press New Shortcut Key" entry box such as ALT used "ALT Before clicking Assign, look where it says “Currently Assigned To”, and make sure you are not about to reasign a really useful shortcut! Make sure Normal.dotm is selected and select "Assign".


2016-02-15 16:48:27

David Goldberg

The macro works great, but how do we assign it to a hotkey combination?


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.