Sorting cells within multiple rows

R

RFJ

I need to sort the cell contents of column 2 of a set of tables (excluding
the header row). Tables have a variable number of rows (four to ten).

The sort sequence is alpha within each cell.

Each cell has a variable number of lines, each line is alphabetic.

Is there a way I can automate it to at least table level since having to do
each cell in turn (where the cells are updated weekly) is a real chore :(


TIA

Robin
 
G

Greg

Robin,

Try:
Sub ScratchMacro()
Dim oTbl As Table
For Each oTbl In ActiveDocument.Tables
oTbl.Columns(2).Sort ExcludeHeader:=True
Next
End Sub
 
R

RFJ

Greg,

Thanks for the suggestion - part way there. It seems to sort all the cells
in the second column as one long list. I need to alpha-sort the entries
within each cell.

Is that an easy adjustment (writing macro code I'm afraid is not something
I've accomplished with any success).
 
G

Greg

Robin,

I didn't have time to test extensively, but try:

Sub ScratchMacro()
Dim oTbl As Table
Dim oCell As Cell
For Each oTbl In ActiveDocument.Tables
oTbl.Columns(2).Sort ExcludeHeader:=True
For Each oCell In oTbl.Columns(2).Cells
oCell.Range.Select
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
On Error Resume Next
Selection.Sort
On Error GoTo 0
Next
Next
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top