+ Reply to Thread
Results 1 to 10 of 10
  1. #1
    Registered User
    Join Date
    06-09-2009
    Location
    Greece
    MS-Off Ver
    Excel 2003/2007
    Posts
    79

    Sort on number of words (words with least characters first)

    I want to sort on number of words, i.e. if a cell contains 1 or 2 words etc, with the cells containing 1 word coming first then cells containing 2 words. Also, if possible, first the cells with fewer characters.
    Last edited by greek; 01-22-2010 at 10:42 PM.

  2. #2
    Registered User
    Join Date
    01-12-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003-7
    Posts
    23

    Re: Sort on number of words (words with least characters first)

    A quick solution if you can stand doing some manual work.
    1. Use a new sheet
    2. Put your data in the FIRST column
    3. Make a copy of this to the THIRD column
    4. In the SECOND row, put a formula =counta(c1:xfd1) (count all nonempty cells from third to the last column) in all cells.
    5. Mark the entire third column. Choose "Data"->"Data Tools"->"Text to columns" (Excel 2007, don't know exactly where in earlier versions, but they all have it). Choose "Delimiter" as space (" ").
    6. Mark the entire sheet. Choose sort and sort by column B.
    7. You now have all your data sorted as you wish.

    Optionally, if you need it sorted in your original sheet, in the original sheet, make a new column, and BEFORE SORTING copy and paste your column TWO (i.e. number of words), paste special "only values". Now you can sort the entire sheet after this column, and when it is done, delete the column. Voila!

    Few manual steps, but very easy! Good luck!
    Last edited by zeke varg; 01-22-2010 at 07:00 PM.

  3. #3
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,228

    Re: Sort on number of words (words with least characters first)

    OK, list of values in column A:
    Code:
    the cat
    a mouse
    My mother
    cat
    dogs
    In B1, put this formula then copy down:
    =LEN(A1)-LEN(SUBSTITUTE(A1," ",""))+1 & "-" & LEN(A1)

    Then highlight both columns, then sort by the B column. End result:
    Code:
    cat  			1-3
    dogs		1-4
    the cat		2-7
    a mouse		2-7
    My mother	2-9
    _________________
    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!)

  4. #4
    Forum Moderator teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    2003 & 2010
    Posts
    10,044

    Re: Sort on number of words (words with least characters first)

    JB, since the output of your formula is text, the sort order will result in lists like the following if there are phrases of 10 words or more

    1-1
    1-2
    11-2
    12-5
    2-5
    20-33
    3-15

    I suggest changing the formula to

    =VALUE(LEN(A1)-LEN(SUBSTITUTE(A1," ",""))+1 & "." & LEN(A1))

    so the result is a number and the sort order is right again.
    Last edited by teylyn; 01-22-2010 at 07:26 PM.
    teylyn
    Microsoft MVP - Excel
    At Excelforum, you can say "Thank you!" by clicking the icon below the post.

    Avoid pie charts with more than two data points. Why? See here (pdf, 559 kb). The only acceptable pie chart is here.

  5. #5
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,379

    Re: Sort on number of words (words with least characters first)

    Teylyn, you would still need to ensure your decimal LEN value was formatted to consistent number of 0's - else a 2 word string of 11 chars (2.11) would be < a 2 word string of 9 chars (2.9)

    =LEN(A1)-LEN(SUBSTITUTE(A1," ",""))+1 & "." & TEXT(LEN(A1),"0000")

    Once the consistency is ensured the value/text issue is moot I think.

  6. #6
    Forum Moderator teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    2003 & 2010
    Posts
    10,044

    Re: Sort on number of words (words with least characters first)

    DO, good point.

    but this
    =LEN(A1)-LEN(SUBSTITUTE(A1," ",""))+1 & "." & TEXT(LEN(A1),"0000")

    Once the consistency is ensured the value/text issue is moot I think.
    is still not perfect. The sort order will be something like

    1.0004
    11.0021
    2.0003

    So, nearing the perfect solution, try

    =VALUE(LEN(A1)-LEN(SUBSTITUTE(A1," ",""))+1 & "." & TEXT(LEN(A1),"0000"))

    which will give numeric values that sort like this

    1.0004
    2.0003
    11.0021
    teylyn
    Microsoft MVP - Excel
    At Excelforum, you can say "Thank you!" by clicking the icon below the post.

    Avoid pie charts with more than two data points. Why? See here (pdf, 559 kb). The only acceptable pie chart is here.

  7. #7
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,379

    Re: Sort on number of words (words with least characters first)

    Sorry, yes, you are of course correct... must coerce resulting output (its 00:15 in my defence!)

  8. #8
    Registered User
    Join Date
    06-09-2009
    Location
    Greece
    MS-Off Ver
    Excel 2003/2007
    Posts
    79

    Re: Sort on number of words (words with least characters first)

    Brilliant guys, works like magic -

  9. #9
    Forum Moderator teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    2003 & 2010
    Posts
    10,044

    Re: Sort on number of words (words with least characters first)

    (its 00:15 in my defence!)
    I know that you can only be upped if extremely distracted or tired!!
    teylyn
    Microsoft MVP - Excel
    At Excelforum, you can say "Thank you!" by clicking the icon below the post.

    Avoid pie charts with more than two data points. Why? See here (pdf, 559 kb). The only acceptable pie chart is here.

  10. #10
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,228

    Re: Sort on number of words (words with least characters first)

    Woo-hoo...Go team!
    _________________
    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!)

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.2.0