Thread: [Solved] Sort By Last Name in a List
View Single Post
 
Old 05-05-2009, 11:39 PM
Bird_FAT's Avatar
Bird_FAT Bird_FAT is offline Office 2007
Expert
 
Join Date: Apr 2009
Location: South East
Posts: 271
Bird_FAT is on a distinguished road
Arrow VB Version

Here's a bit of code that will automate the process for you. It will select all, convert to list, sort by column 2 and then reconvert to text:

Open your VB window (Alt+F11)
In the left hand window, look for 'Normal' and click on the +
Click the + next to Microsoft Word Objects
Double-click ThisDocument
Paste the code below
Import/Open your name list (make sure you only have names in your document)

Run the Macro:
(2003)
Tools > Macro > Macros > (choose 'Sort_Name_List) > Run

(2007)
View > Macro > View Macros > (choose 'Sort_Name_List) > Run


Code:
Sub Sort_Name_List()
'
' 06/05/2009 by Bird
'
    ScreenUpdating = False
    Selection.WholeStory
    Selection.ConvertToTable Separator:=wdSeparateByDefaultListSeparator, _
        NumColumns:=2, NumRows:=7, AutoFitBehavior:=wdAutoFitFixed
    With Selection.Tables(1)
        .Style = "Table Grid"
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = True
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = True
    End With
    Selection.Sort ExcludeHeader:=False, FieldNumber:="Column 2", _
        SortFieldType:=wdSortFieldAlphanumeric, SortOrder:=wdSortOrderAscending, _
        FieldNumber2:="", SortFieldType2:=wdSortFieldAlphanumeric, SortOrder2:= _
        wdSortOrderAscending, FieldNumber3:="", SortFieldType3:= _
        wdSortFieldAlphanumeric, SortOrder3:=wdSortOrderAscending, Separator:= _
        wdSortSeparateByCommas, SortColumn:=False, CaseSensitive:=False, _
        LanguageID:=wdEnglishUK, SubFieldNumber:="Paragraphs", SubFieldNumber2:= _
        "Paragraphs", SubFieldNumber3:="Paragraphs"
    Selection.Rows.ConvertToText Separator:=wdSeparateByDefaultListSeparator, _
        NestedTables:=True
    ScreenUpdating = True
End Sub
By putting this code in the 'ThisDocument' code, then it means that it will be available in all documents.
Reply With Quote