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

I get an error 5992 that the document has mixed cell widths, which is true.
However not for the cells I want to sort.

I think the best overall way to do this is for the Macro to only work on
'selected text'. Can that be done?

Ideally I'd just select each cell I want to sort (as there are actually some
I don't), then run the macro.
--
Thanks.

CJSnet

(Remove TEETH to reply by e-mail.)

"CJSnet" wrote in message
...
Hi Jay, how would I make this work, but just on the *current* cell but no
more?
--
Thanks.

CJSnet

(Remove TEETH to reply by e-mail.)

"Jay Freedman" wrote in message
...
Hi CJ,

Working only in column 3 is a minor variation (look at the For Each
line):

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

For Each oCell In ActiveDocument.Tables(1).Columns(3).Cells
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
Next oCell
End Sub


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

CJSnet wrote:
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?

"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