Written by Allen Wyatt (last updated August 15, 2022)
This tip applies to Word 2007, 2010, and 2013
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:
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.
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!
The entire purpose of macros is to allow you to automate repetitive or tedious tasks with relative ease. How easy the ...
Discover MoreWhen you are using Find and Replace under macro control, you can specify what you want it to do when the end of your ...
Discover MoreA great place for your macro to display status information is, well, in the status bar. Displaying the information is ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
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?
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 © 2023 Sharon Parq Associates, Inc.
Comments