Results 1 to 4 of 4

Improved Word Count Macro

Threaded View

  1. #1
    Registered User
    Join Date
    12-15-2008
    Location
    iop
    Posts
    10

    Improved Word Count Macro

    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!
    Last edited by Amejin; 12-17-2008 at 11:53 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1