F&R or Macro sort on last word in a line for selection

J

Jen

Hi, how do I sort on last word of selection in Word.
I want to select a group of single lines (as below) about 1,000 and sort by
last word only? I know I can I never remember how! Thanks in advance for any
help.

objCC.DropdownListEntries.Add "Dr Edward Rowel"

objCC.DropdownListEntries.Add "Dr Michael Adams"
 
H

Helmut Weber

Hi Jen,

assuming that your lines are paragraphs,
structured like that:
objCC.DropdownListEntries.Add "Dr Edward Rowel"¶
objCC.DropdownListEntries.Add "Dr Michael Adams"¶

Then I'd put the second last word in a paragraph
at the start of that paragraph.
Note that the paragraph mark is the last word
in a paragraph and that the closing quotation mark
is the second last word.
So the name is the third last word:
oprg.range.Words.count - 2

For Each oPrg In Selection.Paragraphs
sTmp = oPrg.Range.Words(oPrg.Range.Words.Count - 2)
oPrg.Range.InsertBefore sTmp & " "
Next

Then I'd sort the paragraphs:
Selection.Sort

Then I'd remove the first word again:
For Each oPrg In Selection.Paragraphs
oPrg.Range.Words(1).Delete
Next


All together:

Sub SortMe()
Dim sTmp As String
Dim oPrg As Paragraph

For Each oPrg In Selection.Paragraphs
sTmp = oPrg.Range.Words(oPrg.Range.Words.Count - 2)
oPrg.Range.InsertBefore sTmp & " "
Next
Selection.Sort
For Each oPrg In Selection.Paragraphs
oPrg.Range.Words(1).Delete
Next
End Sub


Which is far from being perfect,
just quick and dirty,
but setting up a quicksort,
working on the second last word in a paragraph,
was too much for me here and now.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
J

Jen

Thank you Helmet - forgot about the quotation mark - it could have been
dropped but quick and dirty was all that is required.
 

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