Sort according to word length

  • Thread starter perskram attttt gmail dotttt com
  • Start date
P

perskram attttt gmail dotttt com

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!
 
D

Doug Robbins - Word MVP

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


--
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
 
J

Jezebel

Simplest is to paste the list into Excel. Use the Len() function to get the
lengths, then sort on that.
 
D

Doug Robbins - Word MVP

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


--
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
 
P

perskram atttttt gmail dottttt com

Doug said:
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

It worked great! Thanks!

p-e
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top