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

by Allen Wyatt
(last updated February 15, 2016)

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.

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

Changing Line Color in a Drawing Object

Don't like the color of the lines that Excel chose for your drawing object? It's easy to choose your own colors, as ...

Discover More

Printing Summary Information

Word automatically maintains a number of properties for each document you create. As part of those properties you can ...

Discover More

Setting Program Window Size in a Macro

The macro programming language used in Excel gives you a great many tools that allow you to modify the way that Excel ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More WordTips (ribbon)

Bypassing the Startup Macro

Word allows you to create a macro that is run automatically whenever the program is started. If you want to bypass the ...

Discover More

Determining If a Number is Odd or Even

Need to know if a number in a macro is odd or even? It's easy to figure out with the simple calculation shown in this tip.

Discover More

Understanding Precedence

Formulas created in a macro have a specific order in which operations are performed. This is known as precedence, as ...

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}] 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 seven minus 2?

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.

Newest Tips
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.