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

Including Datasheet Rows or Columns in a Chart

Controlling which datasheet information is graphed in a chart.

Discover More

Leading Zeros in Page Numbers

Page numbers in Excel printouts are typically simple counters, without much chance for embellishment. If you want to add ...

Discover More

Creating Long Page Footers

Ever wish that you could create nice, long footers that appear at the bottom of each page when you print your worksheet? ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More WordTips (menu)

Determining the Number of Paragraphs in a Document

When using a macro to process a document in some way, you often need to know the number of paragraphs in the document. (This ...

Discover More

Screen Flip Flop with VBA

Word allows users to conveniently work with multiple documents at the same time. When writing macros, you may need to know ...

Discover More

Determining if Overtype Mode is Active

Your macro may need to determine if the user has overtype mode turned on. You can find out the overtype status easily by ...

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 five minus 4?

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.