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

Freezing Cell Size when Inserting Pictures

Insert a picture into a table cell, and you may quickly find that the table is no longer the size you expected. Here's ...

Discover More

Inserting a Dynamic Word Count in Your Document

Need to know how many words are in your document? You can use the NumWords field to add that statistic, dynamically, to ...

Discover More

Adjusting Row Height for a Number of Worksheets

Adjusting the height of a row or range of rows is relatively easy in Excel. How do you adjust the height of those same ...

Discover More

Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!

More WordTips (ribbon)

Understanding the Select Case Structure

Programming structures are an important tool used by any programmer. The VBA language used by Word's macros includes ...

Discover More

Changing a Macro Description

Part of documenting macros is to provide a good, succinct description of what they do. Changing the description of an ...

Discover More

Displaying the User Name in the Status Bar or Title Bar

Sometimes it can come in handy to know who the current computer user is, as far as Word is concerned. This tip presents ...

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 nine more than 0?

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.