Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2005

Tip: Looking for answers? Try searching our database.

Sort according to word length

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
perskram attttt gmail dotttt com - 19 Oct 2005 17:12 GMT
Does anyone have a clue for a macro that can sort a list of words
according to how many letters there are in the words.
Example: I'd like the list:

bear
do
horse
a
are

to end up like:

a
do
are
bear
horse

thanks to anyone who can help!
--
per-erik
Doug Robbins - Word MVP - 19 Oct 2005 18:57 GMT
The following will do it provided that each word is in a separate paragraph:

Dim i As Long
With ActiveDocument
   .Range.ConvertToTable Separator:=vbCr, AutoFit:=True
   .Tables(1).Columns.Add BeforeColumn:=.Tables(1).Columns(1)
   For i = 1 To .Tables(1).Rows.Count
       .Tables(1).Cell(i, 1).Range = Len(.Tables(1).Cell(i, 2).Range.Text)
   Next i
   .Tables(1).Sort
   .Tables(1).Columns(1).Delete
   .Tables(1).ConvertToText
End With

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> Does anyone have a clue for a macro that can sort a list of words
> according to how many letters there are in the words.
[quoted text clipped - 17 lines]
> --
> per-erik
Jezebel - 19 Oct 2005 22:28 GMT
Simplest is to paste the list into Excel. Use the Len() function to get the
lengths, then sort on that.

> Does anyone have a clue for a macro that can sort a list of words
> according to how many letters there are in the words.
[quoted text clipped - 17 lines]
> --
> per-erik
Doug Robbins - Word MVP - 20 Oct 2005 05:06 GMT
The following will do it provided that each word is in a separate paragraph:

Dim i As Long
With ActiveDocument
   .Range.ConvertToTable Separator:=vbCr, AutoFit:=True
   .Tables(1).Columns.Add BeforeColumn:=.Tables(1).Columns(1)
   For i = 1 To .Tables(1).Rows.Count
       .Tables(1).Cell(i, 1).Range = Len(.Tables(1).Cell(i, 2).Range.Text)
   Next i
   .Tables(1).Sort
   .Tables(1).Columns(1).Delete
   .Tables(1).ConvertToText
End With

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> Does anyone have a clue for a macro that can sort a list of words
> according to how many letters there are in the words.
[quoted text clipped - 17 lines]
> --
> per-erik
perskram atttttt gmail dottttt com - 20 Oct 2005 10:12 GMT
> The following will do it provided that each word is in a separate paragraph:
>
[quoted text clipped - 9 lines]
>     .Tables(1).ConvertToText
> End With

It worked great! Thanks!

p-e
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.