Counting Characters in a Selection with VBA

by Allen Wyatt
(last updated July 9, 2015)

4

When you are developing macros, it is often helpful to know how many characters are in a text selection. The following VBA macro, CharCount, will display the number of characters in a selection.

Sub CharCount()
    Dim Title As String
    Dim CharCount As Integer
    Dim Message As String

    Title = "CharCount"
    CharCount = Len(Selection)
    Message = LTrim(Str(CharCount)) + " character"
    If CharCount <> 1 Then Message = Message + "s"
    MsgBox Message, vbOKOnly, Title
End Sub

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (746) applies to Microsoft Word 97, 2000, 2002, and 2003.

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

Searching and Replacing Graphics

Got a bunch of graphics in a document that need replacing? (Perhaps you need to replace an old logo with a new one.) Word ...

Discover More

Moving from Sheet to Sheet

Need to move quickly through the worksheets in a workbook? Learn the keyboard shortcuts and you can make short work of this ...

Discover More

Understanding Ascending and Descending Sorts

When you sort information, Excel follows a set pattern of how your data is organized. This tip illuminates the burning ...

Discover More

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!

More WordTips (menu)

Determining if Caps Lock is On

If your macro needs to determine the status of the Caps Lock key, you need the code in this tip. Just use the Information ...

Discover More

Printing the Active Document from a Macro

When you process a document in a macro, you may also want to print that document from within the same macro. Here's how to do ...

Discover More

Determining the Number of Pages in Your Document

If your macro needs to know how many pages are in your document, you can use the Information method to get the desired info. ...

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 8Mpixels. 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 eight minus 3?

2015-04-16 10:08:17

B

For i = 1 To wordDocument.Paragraphs.Count
' Get the first 20 characters of the paragraph
paragraphText = wordDocument.Paragraphs(i).Range.Text
If Len(paragraphText) > 20 Then
paragraphText = Left(paragraphText, 20)
End If
' qne put it in the next cell down
ActiveCell.Offset(i - 1, 0) = paragraphText
Next i


2015-01-13 03:11:31

milkan

how to create online counter? http://www.wordcounter.biz/


2014-12-03 14:22:19

Richard Kennedy

Addendum

Len(Selection.Range) returns 0 when no characters are selected.

When characters are selected Len(Selection.Range) returns the same value as Len(Selection), Selection.Characters.Count and Selection.Range.Characters.Count


2014-12-03 14:13:19

Richard Kennedy

You can also obtain character counts using the ComputeStatistics method of the Range object:

CharCount = Selection.Range.ComputeStatistics(wdStatisticCharactersWithSpaces)

(spaces are included in the count, but not carriage returns)

Selection.Range.ComputeStatistics(wdStatisticCharacters)

(spaces are not included in the count, punctuation marks are included)

Len(Selection), Selection.Characters.Count, and
Selection.Range.Characters.Count return Selection.Range.ComputeStatistics(wdStatisticCharactersWithSpaces)
plus carriage return characters.

If no characters are selected, then
Selection.Range.ComputeStatistics(wdStatisticCharactersWithSpaces) = 0 and
Selection.Range.ComputeStatistics(wdStatisticCharacters) = 0, but Len(Selection) = 1, Selection.Characters.Count = 1, and Selection.Range.Characters.Count = 1.

(tested with Word 2013)


This Site

Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon 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.