Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, and 2016. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Counting Words.

Counting Words

by Allen Wyatt
(last updated August 13, 2016)

7

Words are normally associated with a word processor, such as Microsoft Word. However, many people also work with words in their spreadsheet program. (I had a coworker once who used Excel to write memos all the time.) There may be times when you want to count the number of words in a worksheet that you receive from someone. There are native abilities to perform such a task in Word, but not in Excel.

One solution, of course, is to load your workbook into Word, perform the word count there, and then close the file. This is not nearly as flexible, however, as creating a macro to count words within Excel itself. The following macro, CountWords, counts the number of words in any range you select in a worksheet:

Sub CountWords()
    Dim MyRange As Range
    Dim CellCount As Long
    Dim TotalWords As Long
    Dim NumWords As Integer
    Dim Raw As String

    Set MyRange = ActiveSheet.Range(ActiveWindow.Selection.Address)
    TotalWords = 0
    For CellCount = 1 To MyRange.Cells.Count
        If Not MyRange.Cells(CellCount).HasFormula Then
            Raw = MyRange.Cells(CellCount).Value
            Raw = Trim(Raw)
            If Len(Raw) > 0 Then
                NumWords = 1
            Else
                NumWords = 0
            End If
            While InStr(Raw, " ") > 0
                Raw = Mid(Raw, InStr(Raw, " "))
                Raw = Trim(Raw)
                NumWords = NumWords + 1
            Wend
            TotalWords = TotalWords + NumWords
        End If
    Next CellCount
    MsgBox "There are " & TotalWords & " words in the selection."
End Sub

Notice that the macro steps through each cell in the range you select. It then ignores any cell that contains a formula. In all other cells it essentially counts the number of spaces in the cell. (One or more spaces are assumed to separate words.) The word count is then displayed in a message box for your edification.

The macro is pretty quick on relatively small ranges. If you pick a large range (such as the entire worksheet), then the macro can take a great deal of time to finish its work. The point of this is to make sure that you only select the actual range you want to analyze before invoking the macro.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11748) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel here: Counting Words.

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

Bypassing the Startup Macro

Word allows you to create a macro that is run automatically whenever the program is started. If you want to bypass the macro, ...

Discover More

Repeating Table Rows with Manual Page Breaks

Need to make sure part of a table is on one page and part on another? The way to do so is not to use manual page breaks, for ...

Discover More

Turning Off the Clipboard Icon

When you paste information into a document, Word normally displays a small icon to the right of what you pasted. Some people ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Three-Dimensional Transpositions

Excel makes it easy to transpose your data so that rows become columns and columns rows. It doesn't have a built-in ...

Discover More

Automatically Adding 20% to an Entry

When you are developing a worksheet for others to use, you may want to have entries in a particular cell (or cells) be ...

Discover More

Finding Unused Names

After months or years of naming things (such as cell ranges), you may find your workbook cluttered with a bunch of names that ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, 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 7 - 0?

2016-08-19 11:23:05

Willy Vanhaelen

@Thomas Papavasiliou
Thanks for the compliment.
In the following macro I reversed the Trim and Replace functions so the new line characters Chr(10) are replaced with a space first and then the Trim function replaces the double spaces with only one. This corrects the count in the case of a new line followed by a space.

Sub CountWords()
Dim cell As Range, Raw As Variant, X As Integer
For Each cell In Selection
If Not cell.HasFormula Then
X = X + UBound(Split(Application.Trim(Replace(cell, Chr(10), " ")))) + 1
End If
Next cell
MsgBox "There are " & X & " words in the selection."
End Sub

As for your second request (limit rows), years ago I developed a UDF that does just that:

Private Function AdjustedSelection() As Range
Dim Area As Range, Lcc As Integer, Lcr As Long, C As Integer, R As Long
Set AdjustedSelection = Selection.Cells(1) '}
Lcc = Selection.SpecialCells(xlLastCell).Column '}initialize
Lcr = Selection.SpecialCells(xlLastCell).Row '}
For Each Area In Selection.Areas 'loop through all selected areas
R = Area.Rows(Area.Rows.Count).Row 'area's bottommost row
If Lcr < R Then R = Lcr 'if LastCell's row is less: adjust
C = Area.Columns(Area.Columns.Count).Column 'area's rightmost column
If Lcc < C Then C = Lcc 'if LastCell's column is less: adjust
Set AdjustedSelection = Union(AdjustedSelection, Range(Area.Cells(1), Cells(R, C)))
Next Area
End Function

To use it just replace 'For Each cell In Selection' with 'For Each cell In AdjustedSelection'.
On my website (http://wv-be.com/Ribbon.asp?real-life) is explained in detail how it works.


2016-08-18 10:13:06

Thomas Papavasiliou

The macro proposed 13 Aug 2016, by Willy Vanhaelen is brilliant and very fast.

Following a couple of runs I think that two minor improvements would make it even better.

The improvements are:
• Process cases where we have a sequence of Chr(10) and space that gives an erroneous result.
• Limit the rows to process in cases where user selects an entire column. Selecting an entire column is handy if we have data exceeding several screens and containing empty cells.

Regardless of my remarks, I repeat that the macro is brilliant.

I will be very happy if Willy Vanhaelen proposes a new version.


2016-08-13 11:34:51

Brian Canes

If there is a line wrap there is not necessarily a new word. Suppose | represents a newline char(10). If a phrase is "The Trumpire State Building" and it is wrapped like "The Trump|ire State Building" then the number of words is unchanged = 4. It does not become 5.
Regards
Brian


2016-08-13 11:19:02

Brian Canes

UDF (User Defined Function)
Function words(where As Range) As Long
Dim what As Range
With Application
For Each what In where
words = words + .Max(, UBound(Split(.Trim(what.Text), " "))) - (Len(what) > 0)
Next what
End With
End Function
Regards
Brian


2016-08-13 10:47:27

Brian Canes

Here is a single formula to count the number of words in any range named say rng
=SUMPRODUCT(N(LEN(RNG)>1)+LEN(TRIM(RNG))-LEN(SUBSTITUTE(RNG," ","")))
Regards
Brian


2016-08-13 09:28:30

Jean-Pierre Degroote (aka JP ROnse)

Hi,

I believe it can be done with formulas:

=(INT((SUMPRODUCT(LEN(TRIM(A1:A100)))-SUMPRODUCT(LEN(SUBSTITUTE(TRIM(A1:A100)," ",""))))/COUNTA(A1:A100))+1)*COUNTA(A1:A100)+MOD((SUMPRODUCT(LEN(TRIM(A1:A100)))-SUMPRODUCT(LEN(SUBSTITUTE(TRIM(A1:A100)," ","")))),COUNTA(A1:A100))


2016-08-13 07:56:22

Willy Vanhaelen

The macro in this tip is again way to complicated and it has a bug. When some cells contain wrapped text the word count is incorrect.

My mini-macro deals with it:

Sub CountWords()
Dim cell As Range, Raw As Variant, TotalWords As Integer
For Each cell In Selection
If Not cell.HasFormula Then
Raw = Split(Replace(Application.Trim(cell), Chr(10), " "))
TotalWords = TotalWords + UBound(Raw) + 1
End If
Next cell
MsgBox "There are " & TotalWords & " words in the selection."
End Sub

"Simplicity is the ultimate sofistication (Leonardo da Vinci)"


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, 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.