Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021. 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: Character Frequency Count.

Character Frequency Count

Written by Allen Wyatt (last updated July 13, 2024)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021


Scott is looking for a way to get a “frequency count” of all the characters in a document. He would like to know how many times each character, ASCII codes 9 through 255, occur. It is possible to use Find and Replace to determine the count of individual characters (simply search for a character in question and then replace it with itself), but such an approach would be tedious, at best, if you needed to do it for 247 different character codes to get the desired information.

Such a task must be done with a macro, but there are several ways to approach it. One way is to write a macro that will step through each member of the Characters collection, examining each, and assigning that character to one of a number of counters.

Sub CountChars1()
    Dim iCount(0 To 255) As Integer
    Dim i As Integer
    Dim vCharacter As Variant
    Dim sTemp As String

    ' Initialize the array
    For i = 0 To 255
        iCount(i) = 0
    Next i

    ' Fill the array
    For Each oCharacter In ActiveDocument.Characters
       i = Asc(oCharacter)
       iCount(i) = iCount(i) + 1
    Next

    ' Add document for results
    Documents.Add
    Selection.TypeText Text:="ASCII Character Count" & vbCrLf

    ' Only output codes 9 through 255
    For i = 9 To 255
        sTemp = Chr(i)
        If i < 32 Then sTemp = Trim(Str(i))
        sTemp = sTemp & Chr(9) & Trim(Str(iCount(i)))
        sTemp = sTemp & vbCrLf
        Selection.TypeText Text:=sTemp
    Next i
End Sub

The macro uses the iCount array to accumulate the counts of each character code, and then a new document is created to output the results. (The results document can be formatted in any way desired.)

This approach can work well for relatively short documents, up to a few pages. When the document gets longer, the macro gets slower. Why? Because it takes a great deal of time to use the Characters collection for some reason. If the macro runs too slow for your documents, then you will want to change it a bit so that it works solely with strings.

Sub CountChars2()
    Dim iCount(0 To 255) As Long
    Dim i As Long
    Dim j as integer
    Dim lCharCount As Long
    Dim sDoc As String
    Dim sTemp As String

    ' Initialize the array
    For i = 0 To 255
        iCount(i) = 0
    Next i

    ' Assign document to a huge string
    lCharCount = ActiveDocument.Characters.Count
    sDoc = ActiveDocument.Range(0, lCharCount)

    ' Fill the array
    For i = 1 to Len(sDoc)
       j = Asc(Mid(sDoc, i, 1))
       iCount(j) = iCount(j) + 1
    Next

    ' Add document for results
    Documents.Add
    Selection.TypeText Text:="ASCII Character Count" & vbCrLf

    ' Only output codes 9 through 255
    For i = 9 To 255
        sTemp = Chr(i)
        If i < 32 Then sTemp = Trim(Str(i))
        sTemp = sTemp & Chr(9) & Trim(Str(iCount(i)))
        sTemp = sTemp & vbCrLf
        Selection.TypeText Text:=sTemp
    Next i
End Sub

Notice that this version of the macro stuffs the entire document into a single string, sDoc. This string can then be processed very quickly by the macro. (A 635-page document only took about 30 seconds to process on my system.) Because this version is made to work with longer documents, note as well that some of the variable types have been changed to reflect the likelihood of larger counts.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (112) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Word here: Character Frequency Count.

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

Using the INFO Field

The INFO field allows you to include all sorts of information in your document. Here's a quick overview of the field and ...

Discover More

References to Hyperlinks aren't Hyperlinks

Make a reference to a hyperlink in a formula, and you may be surprised that the reference doesn't return an active ...

Discover More

Removing HTTP from URLs

Having problems when it comes to replacing information in URLs? You're not the only one; it can be confusing making mass ...

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)

Creating a New Document in VBA

When working with documents in a macro, it makes sense that you may need to create a document from time to time. Here's ...

Discover More

Trimming Spaces from Strings

When processing text with a macro, you often need to remove extraneous spaces from the text. VBA provides three handy ...

Discover More

Swapping Two Strings

Part of developing macros is learning how to use and manipulate variables. This tip examines a technique you can use to ...

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}] (all 7 characters, in the sequence shown) 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 two more than 7?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


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.

Videos
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.