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

Where Are Word's Settings Stored?

Ever wonder where Word stores all its settings and configuration information? There are only three places where this ...

Discover More

Stepping Through a Non-Contiguous Range of Cells

Using macros to step through each cell in a selection is a common occurrence. What if that selected range is made up of ...

Discover More

Changing Paragraph Borders

Word allows you to easily add borders to a paragraph of text. If you want, you can even change each side of the border to be ...

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 (menu)

Determining Differences Between Dates

Do you need to do some simple math using dates in a your macro? One of the easy functions you can use is the DateDiff ...

Discover More

Determining if a File Exists

Your macro may need to know if a particular file exists. This is easy to figure out using the Dir command, and even easier if ...

Discover More

Turning Off Screen Updating

When working with macros, you can often speed up processing by turning off the updating of the screen. Best news is that it ...

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 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 6 - 0?

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.