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 / May 2008

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jen - 13 May 2008 08:08 GMT
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"
Helmut Weber - 13 May 2008 16:35 GMT
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
Jen - 14 May 2008 00:36 GMT
Thank you Helmet - forgot about the quotation mark - it could have been
dropped but quick and dirty was all that is required.

> Hi Jen,
>
[quoted text clipped - 53 lines]
>
> Vista Small Business, Office XP

Rate this thread:






 
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.