+ Reply to Thread
Results 1 to 4 of 4

Improved Word Count Macro

  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.

    Please Login or Register  to view this content.
    Thanks for your help!
    Last edited by Amejin; 12-17-2008 at 11:53 PM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492
    Macros are fine, but I'm a fan of straight functions. So, here's a formula that will replace all those hidden carraige returns with spaces so your word counts are correct.
    Please Login or Register  to view this content.
    Meanwhile, the CHAR(10) is what you're trying to replace, so maybe you can snug that into your code and do it with your macro.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492
    This macro ONLY does a replace on the Char(10):
    Please Login or Register  to view this content.
    Interestingly, after running this code, my multi-line cells still show in multi-lines, but the word counts are correct after running. Cool.
    (Source)

    You could insert this macro in your sheet separately, then just add a line to call the macro at the top of your current macro:
    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 12-17-2008 at 04:59 AM.

  4. #4
    Registered User
    Join Date
    12-15-2008
    Location
    iop
    Posts
    10
    Thanks for the help guys. I went ahead and came up with this after a few modifications to restrict it to a selection and it seems to work flawlessly. xD

    Please Login or Register  to view this content.

+ Reply to Thread

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