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.
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.
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.
OK, list of values in column A:
In B1, put this formula then copy down:Please Login or Register to view this content.
=LEN(A1)-LEN(SUBSTITUTE(A1," ",""))+1 & "-" & LEN(A1)
Then highlight both columns, then sort by the B column. End result:
Please Login or Register to view this content.
_________________
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!)
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, 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.
My Recommended Reading:
Volatility
Sumproduct & Arrays
Pivot Intro
Email from XL - VBA & Outlook VBA
Function Dictionary & Function Translations
Dynamic Named Ranges
DO, good point.
but thisis still not perfect. The sort order will be something like=LEN(A1)-LEN(SUBSTITUTE(A1," ",""))+1 & "." & TEXT(LEN(A1),"0000")
Once the consistency is ensured the value/text issue is moot I think.
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
Sorry, yes, you are of course correct... must coerce resulting output (its 00:15 in my defence!)
Brilliant guys, works like magic -
I know that you can only be upped if extremely distracted or tired!!(its 00:15 in my defence!)
Woo-hoo...Go team!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks