Table Sorter

Home Up Odds & Ends Photo Gallery Search Contact Me Privacy Notice

 

 

The information in this website is provided without risk or obligation and free of charge.  However, if you have benefitted from my efforts here and would like to make a contribution to help me continue and maintain this work then any donation will be greatly appreciated. Please click the adjacent button to access PayPal.  Thank you.
 
Sorting a list of items in Microsoft Word is simplicity itself.  Take the list of fresh fruits on the left below for example.  You simply select the listed items and use the Table>Sort menu command (or Home>Paragraph>Sort in Word2007) to produce the sorted list show on the right.

If you put that list in a table by selecting the text and using Table>Convert To Text (Insert>Table>Convert To Text in Word2007) the result is always sorted left to right/top to bottom as shown below:

This tips page shows you how to create and sort that table top to bottom/left to right as shown in the next illustration:

The method consists of a VBA project with a simple UserForm and a project module containing procedures to sort rearrange the cell contents from left to right arrangement to a top to botton arrange or vise versa.
The UserForm cosists of two option buttons and a command button.

The UserForm code is:
Private Sub CommandButton1_Click()
Me.Hide
End Sub
The project module code is:
Dim pCell1 As Word.Cell
Dim pCell2 As Word.Cell

Sub TableSorter()

Dim SourceTable As Table
Dim i As Long, j As Long, k As Long
Dim pTmpDoc As Word.Document
Dim pTmpTable As Table
Dim oRng As Word.Range
Dim myFrm As UserForm1

On Error GoTo Err_Handler
Set SourceTable = Selection.Tables(1)
i = SourceTable.Range.Cells.Count
Set myFrm = New UserForm1
myFrm.Show
'Create a temporary document and insert a 1 column/multi-row table
Set pTmpDoc = Documents.Add(Visible:=False)
Set pTmpTable = pTmpDoc.Tables.Add(pTmpDoc.Content, i, 1)
'Fill oTmpTable with contents of the table to be sorted
TableFillAndRefill SourceTable, pTmpTable
'Sort the temporary table
pTmpTable.Sort
'Redefine source table contents based on sort selected sort order
If myFrm.OptionButton1.Value = True Then
    TableFillAndRefill pTmpTable, SourceTable
Else
    With SourceTable
        For i = 1 To .Range.Columns.Count
            For j = 1 To .Range.Rows.Count
                k = k + 1
                Set oRng = pTmpTable.Cell(k, 1).Range
                .Cell(j, i).Range.Text = Left(oRng.Text, Len(oRng.Text) - 2)
            Next j
        Next i
    End With
End If
'Clean up.
Unload myFrm
Set myFrm = Nothing
Set oRng = Nothing
pTmpDoc.Close SaveChanges:=False
Err_Handler:
If Err.Number = 5941 Then
    MsgBox "The cursor must be positioned in the table you want to sort." _
    & vbCr & vbCr & " Position the cursor and run this procedure again."
End If
End Sub

Sub TableFillAndRefill(pTable1 As Table, pTable2 As Table)
'Copies tables cell for cell left to right
Dim pCell1 As Word.Cell
Dim pCell2 As Word.Cell
Set pCell1 = pTable1.Cell(1, 1)
Set pCell2 = pTable2.Cell(1, 1)
Do
    pCell2.Range = Left$(pCell1.Range, Len(pCell1.Range) - 2)
    Set pCell1 = pCell1.Next
    Set pCell2 = pCell2.Next
Loop Until pCell1 Is Nothing
End Sub

I am providing the UserForm and project shown above as a zipped template file:  TableSorter.zip

Unzip the file to your Word>Startup directory if you want to make it load as a global template available to in all your Word documents.  You can click on Installing an Using Global Addins if you need help or further explanation on using templates and addins.

See: Installing Macros for instructions on how to set up and use the macros listed above.

See: Setup Menu/Toolbar for instructions on how to run a macro from a menu or toolbar icon.

See: Keyboard Shortcut for instructions on how to assign a macro to a keyboard shortcut.


Looking for something else?

Google