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

Moving Section Breaks

Section breaks are used to divide a document into two or more sections that can be independently formatting. If you want to ...

Discover More

Understanding Number Formatting Codes

When creating custom formats, you can employ a wide range of codes to define your formatting pattern. This tip focuses on ...

Discover More

Pop-Up Comments for Graphics

Excel allows you to add comments to individual cells in a worksheet, but what if you want to add comments to graphics? Excel ...

Discover More

Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!

More WordTips (ribbon)

Defining a Shortcut for a Macro

You can make running macros very easy if you assign a shortcut key to the macro. This tip demonstrates how easy it is to ...

Discover More

Jumping to the Top of a Page

Do you want to easily jump to the top of a page in your document? You can use the Go To command to make the shift, or you can ...

Discover More

Repaginating Your Document in a Macro

When processing a document with a macro, you may need to have the macro repaginate the text. It's easy to do using the ...

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. 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 four less than 8?

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.