Sort columns of text

  • Thread starter Thread starter Kim
  • Start date Start date
K

Kim

I'm using Word 2007 w/XP. I have a table (3 x 10) with columns of text. I
can sort each column individually but can't sort all columns as a single
sort. There are no blank columns or rows.

What am I missing?

Thanks for any help...........

Kim
 
What do you mean? When you sort on a column, all the information in
that row is sorted according to that column. If you just want to have
three columns of information, but there's no connection between items
in a row, then you don't want to use a table, you want to simply type
lists.

You could copy each column and paste it to a new table, sort it, and
then paste it into the original table?
 
Kim,

Apparently missing is a complete understanding of what exactly you are
trying to do. If (big if) you want to sort the contents of column 1
through 3 as one continous sorted string top to bottom left to right
through the table then the process suggested by Mr. Daniels can be
automated with a macro as follows:

Option Explicit
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
On Error GoTo Err_Handler
Set SourceTable = Selection.Tables(1)
i = SourceTable.Range.Cells.Count
'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
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
'Clean up.
Set oRng = Nothing
pTmpDoc.Close SaveChanges:=False
Exit Sub
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)
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
 
Kim,

Apparently missing is a complete understanding of what exactly you are
trying to do.  If (big if) you want to sort the contents of column 1
through 3 as one continous sorted string top to bottom left to right
through the table

Seems like a good guess. Could you do it with Convert Table to Text
(choosing Return as the separator), then just Sort the result?
 
Do you mean using a macro? Sure, but it would leave a sinlge sorted
list of paragraphs. I simply don't know what output the OP wants.

Sub ScratchMaco()
With Selection
.Tables(1).ConvertToText Separator:=wdSeparateByParagraphs
.Sort
.Paragraphs(1).Range.Delete
Selection.ConvertToTable wdSeparateByParagraphs, 2, 3
End With
End Sub

The reason the other macro looks more complex is if you would try to
put the resulting output of the code above back in a table is would be
sorted left to right top to bottom.
 
Sorry, last macro posted was an attempt to combine the two below and
wouldn't work.

Sorts leaving a single sorted lists

Sub ScratchMaco()
With Selection
.Tables(1).ConvertToText Separator:=wdSeparateByParagraphs
.Sort
.Paragraphs(1).Range.Delete
End With
End Sub

Sorts and returns list to table format sorted left to right/top to bottom.

Sub ScratchMacoII()
With Selection
.Tables(1).ConvertToText Separator:=wdSeparateByParagraphs
.Sort
.MoveStart wdLine, 1
Selection.ConvertToTable wdSeparateByParagraphs, , 3
End With
End Sub
 
Your guess was that Kim wanted a single sorted list of paragraphs --
so unless Kim has lots of such tables, it's more trouble installing
the macro than doing the two steps to sort all the items in the table.
 
Back
Top