
Tips.Net > WordTips Home > Macros > VBA Examples > Counting Characters in a Selection with VBA
Summary: Need to figure out the number of characters in a range of selected text? Here's how to do it in VBA. (This tip works with Microsoft Word 97, Word 2000, Word 2002, and Word 2003.)
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
Tip #746 applies to Microsoft Word versions: 97 2000 2002 2003
Save Time! WordTips has been published weekly since early 1997. Past issues are available in convenient WordTips archives. Have your own enhanced archive of WordTips at your fingertips, available to use at any time!
Check out WordTips Archives today!
Uncover how you can master the full potential of printing your documents. Everything you wanted to know about printing and printers, using Word. (more information...)
Ask a Word Question
Make a Comment
Bugs and Pests Tips
ExcelTips
Family Tips
Health Tips
Home Tips
Organizing Tips
WordTips