Reply
 
Thread Tools Display Modes
  #1   Report Post  
CJSnet
 
Posts: n/a
Default Can't sort items within ONE cell - help please

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 :'(
--
Thanks.

CJSnet

(Remove TEETH to reply by e-mail.)


  #2   Report Post  
Jay Freedman
 
Posts: n/a
Default

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


  #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




  #4   Report Post  
Jay Freedman
 
Posts: n/a
Default

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



  #5   Report Post  
CJSnet
 
Posts: n/a
Default

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







  #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







Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Table Sort - Sort only 3 of 4 columns KGlennC Microsoft Word Help 2 February 26th 05 04:20 PM
Can't sort table alphabetically Tiffany Microsoft Word Help 1 January 21st 05 10:17 PM
word table cell resize or word table cell size change or word table change cell size [email protected] Tables 0 January 13th 05 09:55 PM
copy cell to cell Tlogan Tables 1 January 5th 05 09:20 PM
Shading does not fill entire cell Fred Holmes Tables 1 October 28th 04 10:18 PM


All times are GMT +1. The time now is 10:55 AM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"