Hello all.
Please allow me to explain my situation. At my company I work with a lot of text-filled Excel files. In order to get a proper word count we're currently using the primitive method of "paste into Word and run a word count". I'd like to be able to do this from within Excel, and I found the macro below but have run into a snag.
The problem I have is that it ignores carriage returns and count the last word in the first line and the first word in the following line as one word. In the files I work with this can throw off the count by thousands of words.
So if I run this macro against a cell containing a single text string:
<My name is Amejin and
I like pizza and beer> - 10 words
The macro reads it as:
<My name is Amejin andI like pizza and beer> - 9 words
So, essentially I need a method of changing carriage returns into spaces so that the macro will return the proper count. If anyone has a solution, it would be greatly appreciated.
Sub NumberOfWords()
Dim NumberOfWord As Long
Dim RangeArea As Range
Dim Str As String
Dim Num As Long
For Each RangeArea In ActiveSheet.UsedRange.Cells
Str = Application.WorksheetFunction.Trim(RangeArea.Text)
Num = 0
If Str <> "" Then
Num = Len(Str) - Len(Replace(Str, " ", "")) + 1
End If
NumberOfWord = NumberOfWord + Num
Next RangeArea
MsgBox NumberOfWord
End Sub
Thanks for your help!
Bookmarks