Sort from the right

  • Thread starter Thread starter BK
  • Start date Start date
B

BK

Using Word 2003.

I have a list of names of unequal word count. Some names in the list have
only 1 word, some have 2 words, some have 3 words, some have more. I want
to sort the list by the last name. In Word Perfect you used to be able to
define a sort from the right by using a minus sign. For example, sort by
word -1 which would start counting words from the right. Cannot find that
option in Word 2003, but I'm wondering if there is some other mechanism for
achieving the desired result.

Example list that needs to be sorted by last name:

Benjamin Franklin
John Wilkes Booth
Bill and Hillary Clinton
George Washington
J. Edgar Hoover
Cher
 
I think this might help you. Click the Sort and click Options. Follow
instructions below.

To change the options available, click on the options button on the Sort
Text dialog box. The "Separate fields at" area has three choices: Tabs,
Commas, or Other. Your text is separated by spaces, which puts it in the
"Other" category. Click on Other, put a space in the blank area to the right
of Other, and click on OK. The options available to sort by will now include
Word 1, Word 2, etc. (one word choice for each word in a paragraph).

Because the last name is the second word in each paragraph, you will choose
Word 2 as the field to sort on. Now click on OK, and your lines of text will
be sorted by the last name. If you wanted to sort by the social security
number, you would choose Word 3.

The only time you will have trouble sorting is when there is a variable
number of words before the word you want to sort on. For example, suppose
that in the list above you had typed a middle initial for some individuals
but not for everyone. Now, the last name would sometimes be Word 2 (if there
were no middle initial) and sometimes be Word 3 (if there were a middle
initial). In other words, the initial would be considered the second "word"
in the paragraph.

Ardell
 
Thanks for the input, but the problem I have is the very one you describe at
the bottom of your message. The last name is not always Word 2 when
counting from the left. (See the sample list of names I included in my
original message.) The only consistent thing about the last name is that it
is always the last word on the line. In Word Perfect, that made it "Word
Number -1" which started counting from the right backwards because of the
minus sign. I was hoping there would be something similar in Word.
 
You would have to add a Hard Space (Control Shift Space) between some of the
words to make it act as one word. So, if you had J. Edgar Hoover (which is 3
words), add (Control Shift Space) between the J. and Edgar. Then you have 2
words and you can control the sort. There's no simple minus code like WP
had.
 
The following code sorts your list to:

John Wilkes Booth
Cher
Bill and Hillary Clinton
Benjamin Franklin
J. Edgar Hoover
George Washington

Go to Tools, Macros, Macros, Create and copy from sub to end sub below and
paste to replace newly created empty sub. Return to your doc, select your
list, go to tools, macros, macros, select lastwordsort and click run. It will
add a tab character before the last word in each line, sort by field 2, and
then delete the tabs.

Sub LastWordSort()
Set bkstrange = Selection.Range
For Each p In bkstrange.Paragraphs
p.Range.Select
Selection.EndKey Unit:=wdLine
Selection.MoveLeft Unit:=wdWord, Count:=1
Selection.TypeText Text:=vbTab
Next p
bkstrange.Select
Selection.Sort FieldNumber:="Field 2"
With Selection.Find
.Text = "^t"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub
 
Thanks, I'll give it a try.



BoniM said:
The following code sorts your list to:

John Wilkes Booth
Cher
Bill and Hillary Clinton
Benjamin Franklin
J. Edgar Hoover
George Washington

Go to Tools, Macros, Macros, Create and copy from sub to end sub below and
paste to replace newly created empty sub. Return to your doc, select your
list, go to tools, macros, macros, select lastwordsort and click run. It
will
add a tab character before the last word in each line, sort by field 2,
and
then delete the tabs.

Sub LastWordSort()
Set bkstrange = Selection.Range
For Each p In bkstrange.Paragraphs
p.Range.Select
Selection.EndKey Unit:=wdLine
Selection.MoveLeft Unit:=wdWord, Count:=1
Selection.TypeText Text:=vbTab
Next p
bkstrange.Select
Selection.Sort FieldNumber:="Field 2"
With Selection.Find
.Text = "^t"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub
 
Back
Top