+ Reply to Thread
Results 1 to 18 of 18

VBA code to count unique words in a range of cells

  1. #1
    Registered User
    Join Date
    08-19-2014
    Location
    SF, CA
    MS-Off Ver
    2010
    Posts
    8

    VBA code to count unique words in a range of cells

    Hello, I am trying to find a way to count the number of unique words in a range of cells when each cell contains numerous words. For example:

    The dog and the cat are asleep.
    The cat is inside.
    The dog is outside.

    Each of the above sentences would be in their own cell. I'm looking for a way to count the unique words in the above range, which should be 9 for "The, dog, and, cat, are, asleep, is, inside, outside". I've found formulas to count unique cells, but not unique words. Ideally, I want to put this into VBA so that all I would need to do is highlight a range and run the macro.

    Is this possible? Can anyone offer some assistance?

    Thanks.

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: VBA code to count unique words in a range of cells

    Try
    Please Login or Register  to view this content.

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: VBA code to count unique words in a range of cells

    Duplicate........

  4. #4
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: VBA code to count unique words in a range of cells

    Hi,

    If I gave you a purely formula-based solution would you be able to then write that into VBA?

    Regards
    Click * below if this answer helped

    Advanced Excel Techniques: http://excelxor.com/

  5. #5
    Registered User
    Join Date
    08-19-2014
    Location
    SF, CA
    MS-Off Ver
    2010
    Posts
    8

    Re: VBA code to count unique words in a range of cells

    @Jindon, your code comes close, but it is counting how many times each unique word appears and printing out all of the unique words. Like so:
    FirstTry.jpg

    That would give a total of 15. I don't need to know how many times each word appears. Is there a way to weed that out? I could do without it printing all of the unique words as well, but that's not necessary.

    Quote Originally Posted by jindon View Post
    Try
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    08-19-2014
    Location
    SF, CA
    MS-Off Ver
    2010
    Posts
    8

    Re: VBA code to count unique words in a range of cells

    @XOR LX, I could work with something purely formula-based.

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: VBA code to count unique words in a range of cells

    Please Login or Register  to view this content.

  8. #8
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: VBA code to count unique words in a range of cells

    Ok, though I imagine it won't be half as efficient as some code...

    First go to Name Manager (Formulas tab) and define the following:

    Name: Range1
    Refers to: =$A$1:$A$3

    (Or whatever happens to be the range in question.)

    Name: Arry1
    Refers to: =1+LEN(Range1)-LEN(SUBSTITUTE(Range1," ",""))

    Name: Arry2
    Refers to: =ROW(INDIRECT("1:"&(MAX(Arry1)*ROWS(Range1))))

    Name: Arry3
    Refers to: =INDEX(TRIM(MID(SUBSTITUTE(Range1," ",REPT(" ",999)),TRANSPOSE(999*(ROW(INDIRECT("1:"&MAX(Arry1)))-1)+1),999)),N(IF(1,1+INT((Arry2-1)/MAX(Arry1)))),N(IF(1,1+MOD(Arry2-1,MAX(Arry1)))))

    Exit Name Manager.

    The required array formula** is then:

    =SUM((Arry3<>"")/MMULT(0+(Arry3=TRANSPOSE(Arry3)),ROW(INDIRECT("1:"&COUNTA(Arry3)))^0))

    Regards


    **Array formulas are not entered in the same way as 'standard' formulas. Instead of pressing just ENTER, you first hold down CTRL and SHIFT, and only then press ENTER. If you've done it correctly, you'll notice Excel puts curly brackets {} around the formula (though do not attempt to manually insert these yourself).

  9. #9
    Registered User
    Join Date
    08-19-2014
    Location
    SF, CA
    MS-Off Ver
    2010
    Posts
    8

    Re: VBA code to count unique words in a range of cells

    @jindon, Thanks! This seems to be a great start. I made a couple modifications to increase the versatility (allowing the user to use a selected range. I think this will do nicely. Thanks again!

    Please Login or Register  to view this content.
    Last edited by scottyblaze; 08-19-2014 at 06:49 PM.

  10. #10
    Registered User
    Join Date
    08-19-2014
    Location
    SF, CA
    MS-Off Ver
    2010
    Posts
    8

    Re: VBA code to count unique words in a range of cells

    @XOR LX, Thanks, but jindon's solution will be much easier for me to share with some of the other's who are not Excel savvy at all. I appreciate the effort.

  11. #11
    Registered User
    Join Date
    08-19-2014
    Location
    SF, CA
    MS-Off Ver
    2010
    Posts
    8

    Re: VBA code to count unique words in a range of cells

    @jindon, I know this would be a bit out of scope, but do you know a way to set this so it grabs the values from all column A's in the workbook?

    Quote Originally Posted by jindon View Post
    Please Login or Register  to view this content.

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: VBA code to count unique words in a range of cells

    scottyblaze

    Before anything, please edit your post #9.
    VBA code should wrapped with code tags.

    i.e
    [code]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/code]

    and full quotes should be added only if it is really necessary.

  13. #13
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: VBA code to count unique words in a range of cells

    OK thanks for the edit
    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    08-19-2014
    Location
    SF, CA
    MS-Off Ver
    2010
    Posts
    8

    Re: VBA code to count unique words in a range of cells

    Sorry about that. I'm a bit new here. I'll try to be more mindful.

  15. #15
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: VBA code to count unique words in a range of cells

    Did you try the code in my previous post?

  16. #16
    Registered User
    Join Date
    08-19-2014
    Location
    SF, CA
    MS-Off Ver
    2010
    Posts
    8

    Re: VBA code to count unique words in a range of cells

    I was just able to try it out. It looks like a winner! I can't thank you enough.

  17. #17
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: VBA code to count unique words in a range of cells

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

  18. #18
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: VBA code to count unique words in a range of cells

    Cross Posted here:
    http://www.mrexcel.com/forum/excel-q...nge-cells.html

    A message to forum cross posters

    Please read this:
    http://www.excelguru.ca/node/7
    Have a great day,
    Stan

    Windows 10, Excel 2007, on a PC.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved by clicking EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 3
    Last Post: 06-25-2014, 03:28 AM
  2. [SOLVED] Find unique values from range ( populated cells only) & not count zeros or NA errors VBA
    By KK1234 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-12-2014, 06:50 AM
  3. Count unique words appearing in a column once per row
    By eq2 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 01-19-2010, 12:09 PM
  4. count unique cells in range based on date
    By leedsd75 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 10-09-2007, 02:04 PM
  5. Genarating count of unique words in a cell or cells
    By Hari in forum Excel Programming / VBA / Macros
    Replies: 23
    Last Post: 01-28-2005, 03:06 AM

Tags for this Thread

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