Loading
Word.Tips.Net WordTips (Menu Interface)

Generating a Count of Word Occurrences

Please Note: This article is written for users of the following Microsoft Word versions: 97, 2000, 2002, and 2003. If you are using a later version (Word 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Word, click here: Generating a Count of Word Occurrences.

As you are analyzing your documents, you may wonder if there is a way to create a count of the number of words in the document. Unfortunately, Word doesn't include such a feature, but there are a couple of things you can do.

First, if you want to know the number of times a specific word or phrase is used, you can follow these steps:

  1. Press Ctrl+H to display the Replace tab of the Find and Replace dialog box. (See Figure 1.)
  2. Figure 1. The Replace tab of the Find and Replace dialog box.

  3. In the Find What box, enter the word or phrase you want counted.
  4. In the Replace With box, enter ^&. This character sequence tells Word that you want to replace what you find with whatever you placed in the Find What box. (In other words, you are replacing the word or phrase with itself.)
  5. If you are searching for individual words, make sure you click the Find Whole Words Only check box.
  6. Click on Replace All. Word makes the replacements and shows you how many instances it replaced. That is the number you want.

This approach works great if you just have one or two words or phrases you want to know about. You can automate the process a bit by using a macro to search through the document and count for you. The following macro prompts the user for a word, and then counts the number of times that word appears in the document. It will continue to ask for another word until the user clicks on the Cancel button.

Sub FindWords()
    Dim sResponse As String
    Dim iCount As Integer

    ' Input different words until the user clicks cancel
    Do
        ' Identify the word to count
        sResponse = InputBox( _
          Prompt:="What word do you want to count?", _
          Title:="Count Words", Default:="")
    
        If sResponse > "" Then
            ' Set the counter to zero for each loop
            iCount = 0
            Application.ScreenUpdating = False
            With Selection
                .HomeKey Unit:=wdStory
                With .Find
                    .ClearFormatting
                    .Text = sResponse
                    ' Loop until Word can no longer
                    ' find the search string and
                    ' count each instance
                    Do While .Execute
                        iCount = iCount + 1
                        Selection.MoveRight
                    Loop
                End With
                ' show the number of occurences
                MsgBox sResponse & " appears " & iCount & " times"
            End With
            Application.ScreenUpdating = True
        End If
    Loop While sResponse <> ""
End Sub

If you want to determine all the unique words in a document, along with how many times each of them appears in the document, then a different approach is needed. The following VBA macro will do just that.

Sub WordFrequency()
    Const maxwords = 9000          'Maximum unique words allowed
    Dim SingleWord As String       'Raw word pulled from doc
    Dim Words(maxwords) As String  'Array to hold unique words
    Dim Freq(maxwords) As Integer  'Frequency counter for unique words
    Dim WordNum As Integer         'Number of unique words
    Dim ByFreq As Boolean          'Flag for sorting order
    Dim ttlwds As Long             'Total words in the document
    Dim Excludes As String         'Words to be excluded
    Dim Found As Boolean           'Temporary flag
    Dim j, k, l, Temp As Integer   'Temporary variables
    Dim ans As String              'How user wants to sort results
    Dim tword As String            '

    ' Set up excluded words
    Excludes = "[the][a][of][is][to][for][by][be][and][are]"

    ' Find out how to sort
    ByFreq = True
    ans = InputBox("Sort by WORD or by FREQ?", "Sort order", "WORD")
    If ans = "" Then End
    If UCase(ans) = "WORD" Then
        ByFreq = False
    End If
    
    Selection.HomeKey Unit:=wdStory
    System.Cursor = wdCursorWait
    WordNum = 0
    ttlwds = ActiveDocument.Words.Count

    ' Control the repeat
    For Each aword In ActiveDocument.Words
        SingleWord = Trim(LCase(aword))
        'Out of range?
        If SingleWord < "a" Or SingleWord > "z" Then
            SingleWord = ""
        End If
        'On exclude list?
        If InStr(Excludes, "[" & SingleWord & "]") Then
            SingleWord = ""
        End If
        If Len(SingleWord) > 0 Then
            Found = False
            For j = 1 To WordNum
                If Words(j) = SingleWord Then
                    Freq(j) = Freq(j) + 1
                    Found = True
                    Exit For
                End If
            Next j
            If Not Found Then
                WordNum = WordNum + 1
                Words(WordNum) = SingleWord
                Freq(WordNum) = 1
            End If
            If WordNum > maxwords - 1 Then
                j = MsgBox("Too many words.", vbOKOnly)
                Exit For
            End If
        End If
        ttlwds = ttlwds - 1
        StatusBar = "Remaining: " & ttlwds & ", Unique: " & WordNum
    Next aword

    ' Now sort it into word order
    For j = 1 To WordNum - 1
        k = j
        For l = j + 1 To WordNum
            If (Not ByFreq And Words(l) < Words(k)) _
              Or (ByFreq And Freq(l) > Freq(k)) Then k = l
        Next l
        If k <> j Then
            tword = Words(j)
            Words(j) = Words(k)
            Words(k) = tword
            Temp = Freq(j)
            Freq(j) = Freq(k)
            Freq(k) = Temp
        End If
        StatusBar = "Sorting: " & WordNum - j
    Next j

    ' Now write out the results
    tmpName = ActiveDocument.AttachedTemplate.FullName
    Documents.Add Template:=tmpName, NewTemplate:=False
    Selection.ParagraphFormat.TabStops.ClearAll
    With Selection
        For j = 1 To WordNum
            .TypeText Text:=Trim(Str(Freq(j))) _
              & vbTab & Words(j) & vbCrLf
        Next j
    End With
    System.Cursor = wdCursorNormal
    j = MsgBox("There were " & Trim(Str(WordNum)) & _
      " different words ", vbOKOnly, "Finished")
End Sub

When you open a document and run this macro, you are asked if you want to create a list sorted by word or by frequency. If you choose word, then the resulting list is shown in alphabetical order. If you choose frequency, then the resulting list is in descending order based on how many times the word appeared in the document.

While the macro is running, the status bar indicates what is happening. Depending on the size of your document and the speed of your computer, the macro may take a while to complete. (I ran it with a 719-page document with over 349,000 words and it took about five minutes to complete.)

Note that there is a line in the macro that sets a value in the Excludes string. This string contains words that the macro will ignore when putting together the word list. If you want to add words to the exclusion list, simply add them to the string, between [square brackets]. Also, make sure the exclusion words are in lowercase.

If you don't like to use macros for some reason, there are other programs you can use to create word counts. For instance, the NoteTab text editor (the "light" version can be downloaded free at http://www.notetab.com) includes a feature that provides a word count. All you need to do is copy your entire document and paste it into NoteTab. Then, within NoteTab, choose Tools | Text Statistics | More. It presents an analysis of the word frequency, including percentages.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (1833) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Generating a Count of Word Occurrences.

Related Tips:

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 them! Check out Microsoft Word 2013 Step by Step today!

 

Comments for this tip:

Scooter Dee    26 Oct 2014, 09:17
Thank you! This macro worked like a champ and was exactly what I needed!
Chris    07 Oct 2014, 07:10
Is there a relatively easy way to make this macro work for Arabic script?
Thanks!
Duncan    08 Jan 2014, 13:58
Brilliant!! This helped me so much! I want to donate.
AJC    22 Aug 2013, 09:35
Excellent and easy to implement code. Thank you very much!
Sandy    31 Jul 2013, 16:19
This is so great, thank-you ^_^. Now I can spot words I tend to overuse more easily.
Jeremy    24 Jul 2013, 08:57
This was awesom for finding the words, but I also need to find form numbers.
Gumpert    05 Jun 2013, 09:03
Very useful base. My particular use case was trying to track down different syntax of the same word in a draft of an upcoming specification. The intention is to eliminate variation - not an easy task when multiple people are writing different sections of the specification. I wanted to be able to identify ReadAll as opposed to READALL, read-all, read_all, or read all so that we could converge on a common syntax. The output did have to be 'manually parsed', but this made life a ton easier and should drastically improve the quality of the document.



Sub WordFrequency()
    Const maxwords = 9000 'Maximum unique words allowed
    Dim SingleWord As String 'Raw word pulled from doc
    Dim Words(maxwords) As String 'Array to hold unique words
    Dim Freq(maxwords) As Integer 'Frequency counter for unique words
    Dim WordNum As Integer 'Number of unique words
    Dim ByFreq As Boolean 'Flag for sorting order
    Dim ttlwds As Long 'Total words in the document
    Dim Excludes As String 'Words to be excluded
    Dim Punctuation As String 'punctuantion that's used to break up compound words (ie hyphen, underscore, etc)
    Dim Found As Boolean 'Temporary flag
    Dim j, k, l, Temp As Integer 'Temporary variables
    Dim ans As String 'How user wants to sort results
    Dim tword, nextWord, currentWord, lastWord As String '
    Dim buildingHyphen, HyphenUsed As Boolean
    

    ' Set up excluded words
    Excludes = "[the][a][of][is][to][for][by][be][and][are]"
    Punctuation = "[-][_][']"

    ' Find out how to sort
    ByFreq = True
    ans = InputBox("Sort by WORD or by FREQ?", "Sort order", "WORD")
    If ans = "" Then End
    If UCase(ans) = "WORD" Then
        ByFreq = False
    End If
    
    Selection.HomeKey Unit:=wdStory
    System.Cursor = wdCursorWait
    WordNum = 0
    ttlwds = ActiveDocument.Words.Count
    
    nextWord = ""
    currentWord = ""
    lastWord = ""
    buildingHyphen = False
    HyphenUsed = False
    HyphenWordNum = 0
    
    'generate a list of all words in the document as well as the number of times they occur
    For Each aword In ActiveDocument.Words
        If nextWord = "" Then
            nextWord = Trim(aword)
        Else
            currentWord = nextWord
            nextWord = Trim(aword)
                
            If InStr(Punctuation, "[" & currentWord & "]") Then
                buildingHyphen = True
                lastWord = lastWord + Trim(currentWord)
                HyphenUsed = True
            ElseIf buildingHyphen Then
                buildingHyphen = False
                lastWord = lastWord + Trim(currentWord)
            Else
                buildingHyphen = False
                lastWord = Trim(currentWord)
            End If
            
            If Not buildingHyphen And InStr(Punctuation, "[" & nextWord & "]") = False And ((lastWord >= "a" And lastWord <= "z") Or (lastWord >= "A" And lastWord <= "Z")) Then
                SingleWord = Trim(lastWord)
                                                            
                If InStr(Excludes, "[" & LCase(SingleWord) & "]") Then
                    SingleWord = ""
                End If
                
                If Len(SingleWord) > 0 Then
                    Found = False
                    For j = 1 To WordNum
                        If Words(j) = SingleWord Then
                            Freq(j) = Freq(j) + 1
                            Found = True
                            Exit For
                        End If
                    Next j
                    If Not Found Then
                        WordNum = WordNum + 1
                        Words(WordNum) = SingleWord
                        Freq(WordNum) = 1
                    End If
                    If WordNum > maxwords - 1 Then
                        j = MsgBox("Too many words.", vbOKOnly)
                        Exit For
                    End If
                End If
            End If
        End If
        
        ttlwds = ttlwds - 1
        StatusBar = "step 1 Remaining: " & ttlwds & ", Unique: " & WordNum
    Next aword
    
    ttlwds = ActiveDocument.Words.Count
    
    nextWord = ""
    
    ' Loop again, this time looking for words that might have a space instead of a punctuation mark (ie "read all" as opposed to "read-all")
    For Each aword In ActiveDocument.Words
        If nextWord = "" Then
            nextWord = Trim(aword)
        Else
            currentWord = nextWord
            nextWord = Trim(aword)
         
            'see if the two words were found in the punctuated word array (an example would be "read all" and "read-all")
            Found = False
            
            'try to avoid doing a multitude of operations when not all are always necessary
            If Len(currentWord) > 1 And Len(nextWord) > 1 Then
                If LCase(currentWord) <> LCase(nextWord) Then
                    For j = 1 To WordNum
                        If LCase(currentWord) <> LCase(Words(j)) And LCase(nextWord) <> LCase(Words(j)) Then
                            If InStr(LCase(Words(j)), LCase(currentWord)) > 0 And InStr(LCase(Words(j)), LCase(nextWord)) > 0 Then
                                SingleWord = currentWord + " " + nextWord
                                                            
                                Found = True
                                Exit For
                            End If
                        End If
                    Next j
                End If
            End If
        
            If Found = True Then
                'j = MsgBox("found : " + SingleWord + " in: " + Words(j), vbOKOnly)
                
                'reset variable
                Found = False
                
                For j = 1 To WordNum
                    If Words(j) = SingleWord Then
                        Freq(j) = Freq(j) + 1
                        Found = True
                        Exit For
                    End If
                Next j
                If Not Found Then
                    WordNum = WordNum + 1
                    Words(WordNum) = SingleWord
                    Freq(WordNum) = 1
                End If
                If WordNum > maxwords - 1 Then
                    j = MsgBox("Too many words.", vbOKOnly)
                    Exit For
                End If
            End If
        End If
        
        ttlwds = ttlwds - 1
        StatusBar = "step 2 Remaining: " & ttlwds & ", Unique: " & WordNum
    Next aword

    ' Now sort it into word order
    Dim temp1, temp2 As String
    
    For j = 1 To WordNum - 1
        k = j
        For l = j + 1 To WordNum
            If (Not ByFreq And LCase(Words(l)) < LCase(Words(k))) _
              Or (ByFreq And Freq(l) > Freq(k)) Then k = l
        Next l
        If k <> j Then
            tword = Words(j)
            Words(j) = Words(k)
            Words(k) = tword
            Temp = Freq(j)
            Freq(j) = Freq(k)
            Freq(k) = Temp
        End If
        StatusBar = "Sorting: " & WordNum - j
    Next j

    ' Now write out the results
    tmpName = ActiveDocument.AttachedTemplate.FullName
    Documents.Add Template:=tmpName, NewTemplate:=False
    Selection.ParagraphFormat.TabStops.ClearAll
    With Selection
        For j = 1 To WordNum
            .TypeText Text:=Trim(Str(Freq(j))) _
              & vbTab & Words(j) & vbCrLf
        Next j
    End With
    System.Cursor = wdCursorNormal
    j = MsgBox("There were " & Trim(Str(WordNum)) & _
      " different words ", vbOKOnly, "Finished")
End Sub

Mara-mirax    30 May 2013, 10:58
AWESOME! This is exactly what I was looking for - thanks so much :-D
John    16 May 2013, 13:27
Thank you - worked perfectly and vairrrry useful!
Laura    23 Apr 2013, 16:23
Thank you. Your karma level should be off the charts!
Bob    28 Jan 2013, 22:37
This is exactly what I was looking for. Worked perfectly.
Thanks!
ezra    26 Jan 2013, 21:12
This was amazing! The macro worked on the first try....and saved hours of work. thank you!
Chris    05 Jan 2013, 17:46
FANTASTIC! Works like a charm

1 charm
1 fantastic
1 like
1 works
John    07 Dec 2012, 10:30
Thank you for posting this. it was an immense help!
Rachelle    23 Oct 2012, 17:41
Awesome! :)
Alex    22 Oct 2012, 17:48
Awesome, worked perfectly!

Needed it to see how many orders we had with each individual client, but submissions were in standard emails!!!!

AHHHH!!!!

Next step is make a form for them to submit LOL.

Josh, why can't you just search two times for each one, instead of creating a custom macro to do that for you?

Unless you have to do it like 100 times a day that would not be good.
Dave N    06 Oct 2012, 12:01
THANK YOU for posting this - crazy helpful. Just created my first macro!
Martin    05 Sep 2012, 15:59
I'm getting an error message in the unique word micro. It's saying: Variable not defined and it's highlighting aword under ' Control the repeat section.
Josh    25 Jun 2012, 09:36
Is there a way to edit your first macro (the FindWords macro) so that you can search for two words/phrases?


Basically, I need to compare how often two phrases appear. So for example I input that the two words are 'Start Soundbite' and 'End Soundbite' and the macro will tell me how often the both appear.

Any help you could give me would be greatly appreciated!
Subin    20 Jun 2012, 00:52
Thanks a lot - worked like a breeze !

Leave your own comment:

*Name:
Email:
  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*Text:
*What is 4+5 (To prevent automated submissions and spam.)
 
          Commenting Terms
 
 

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us

 

Advertise with Us

Our Privacy Policy

Our Sites

Tips.Net

Beauty and Style

Cars

Cleaning

Cooking

DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2013)

Gardening

Health

Home Improvement

Money and Finances

Organizing

Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2013)

Our Products

Premium Newsletters

Helpful E-books

Newsletter Archives

 

Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2014 Sharon Parq Associates, Inc.