View Single Post
  #3   Report Post  
CJSnet
 
Posts: n/a
Default

Wow, that's great Jay thanks.

The only thing I should mention is that I have 3 columns and only want the
words in the 3rd column sorted. Is there a mod to the macro that instead of
moving onto the next cell will move down a cell?

Alternatively can I just run the macro and have it stop after it's sorted
the current cell, then I can run it again on the next relevant cell?
--
Thanks.

CJSnet

(Remove TEETH to reply by e-mail.)


"Jay Freedman" wrote in message
...
CJSnet wrote:
Hi, I have a very long table with lots of cells. Each cell has many
words within it, separated by commas. E.g.:

one, two, three, four, five, six, seven...

I need to sort the words in each cell, alphabetically. Any idea on
earth how I can do this? Doesn't seem to wanna let me :'(


Hi CJ,

The Sort command can sort paragraphs, but it can't sort within a single
paragraph. You have to split the list into separate paragraphs, sort, and
then put them back together.

Here's a macro that should do what you want. In each cell, it used
search&replace to change each comma-and-space into a paragraph mark, does
the sort on that cell, replaces the paragraph marks with comma-and-space,
and then moves on to the next cell. Before you start the macro, make sure
there aren't any paragraph marks in the table (use the ¶ button to display
nonprinting characters).

Sub SortInCells()
Dim oRg As Range
Dim oCell As Cell

Set oCell = ActiveDocument.Tables(1).Cell(1, 1)

Do
Set oRg = oCell.Range
oRg.MoveEnd unit:=wdCharacter, Count:=-1
With oRg.Find
.Text = ", "
.Replacement.Text = "^p"
.Wrap = wdFindStop
.Execute Replace:=wdReplaceAll
End With

oRg.Sort

Set oRg = oCell.Range
oRg.MoveEnd unit:=wdCharacter, Count:=-1
With oRg.Find
.Text = "^p"
.Replacement.Text = ", "
.Wrap = wdFindStop
.Execute Replace:=wdReplaceAll
End With

Set oCell = oCell.Next
Loop Until (oCell Is Nothing)
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org