#1   Report Post  
RFJ
 
Posts: n/a
Default Sort Question

I have a table of multiple rows and columns, In each cell of one column are
multiple text entries (each entry on a new line).

What I want to do is to be able sort the entries in each cell (of just this
one column) so that they are in alpha order within each cell.

I can do it manually (but there are over sixty of them - and the table keeps
changing ) so I then have to find any cells that have changed and
re-sort.

Is there a way I can do it in a more automated and less error prone way.

TIA

Rob


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

Hi Rob,

First, make sure that there's a paragraph mark (not a manual line break) at
the end of each line within the cell -- the sort function depends on this.
If you can sort the cell manually, then you're OK with this.

Put the following macro into a template, using the instructions at
http://www.gmayor.com/installing_macro.htm if needed. You didn't say which
column of the table you need to sort -- change the (3) to the correct column
number.

Sub SortWithinCells()
' separately sort the contents of each cell
' in a specific column of a table
Dim oRow As Row
Dim oRg As Range

If Not Selection.Information(wdWithInTable) Then
MsgBox "Please put the cursor in the table" _
& vbCr & "and restart the macro"
Exit Sub
End If

For Each oRow In Selection.Tables(1).Rows
' sort the contents of the cell in column 3
Set oRg = oRow.Cells(3).Range
oRg.MoveEnd wdCharacter, -1
oRg.Sort
Next oRow
End Sub

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

RFJ wrote:
I have a table of multiple rows and columns, In each cell of one
column are multiple text entries (each entry on a new line).

What I want to do is to be able sort the entries in each cell (of
just this one column) so that they are in alpha order within each
cell.

I can do it manually (but there are over sixty of them - and the
table keeps changing ) so I then have to find any cells that have
changed and re-sort.

Is there a way I can do it in a more automated and less error prone
way.

TIA

Rob



  #3   Report Post  
RFJ
 
Posts: n/a
Default

Hi Jay,

Tx for the response - sorry about the delay (been away)

I've got the macro installed but an error message is being generated as
follows

Runtime error "5280"
Word found no valid records to sort

Debug is highlighting the line oRg.Sort

My position on the macro learning curve is almost at the bottom so I'm stuck
as to what to do next. All I know is :
- a table is /definitely/ there
- I want to sort on Column 2 so have changed the code accordingly.

Any further help would be much appreciated.

Rob


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

First, make sure that there's a paragraph mark (not a manual line break)

at

cut


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

Hi Rob,

Now I see what's probably happening. Do you have one or more rows where
there's only one line in the cell in Column 2? That will cause that error
message (oddly, having an empty cell isn't any problem).

The fix is simple. Add the line "On Error Resume Next" to the macro as shown
in the segment below:

For Each oRow In Selection.Tables(1).Rows
' sort the contents of the cell in column 2
On Error Resume Next
Set oRg = oRow.Cells(2).Range
oRg.MoveEnd wdCharacter, -1
oRg.Sort
Next oRow
End Sub

That will make the macro ignore any cells it can't sort.

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

RFJ wrote:
Hi Jay,

Tx for the response - sorry about the delay (been away)

I've got the macro installed but an error message is being generated
as follows

Runtime error "5280"
Word found no valid records to sort

Debug is highlighting the line oRg.Sort

My position on the macro learning curve is almost at the bottom so
I'm stuck as to what to do next. All I know is :
- a table is /definitely/ there
- I want to sort on Column 2 so have changed the code accordingly.

Any further help would be much appreciated.

Rob


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

First, make sure that there's a paragraph mark (not a manual line
break) at


cut



  #5   Report Post  
RFJ
 
Posts: n/a
Default

Thanks Jay (particularly for the fast response) - its working now with that
extra line BG

Interestingly, if there is an empty cell in column 2 (the one I'm sorting
on), the sort defaults to column 1. But that was a problem easily overcome
by putting a single dash in the cell.

Regards

Rob



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

Now I see what's probably happening. Do you have one or more rows where
there's only one line in the cell in Column 2? That will cause that error
message (oddly, having an empty cell isn't any problem).

The fix is simple. Add the line "On Error Resume Next" to the macro as

shown
in the segment below:

For Each oRow In Selection.Tables(1).Rows
' sort the contents of the cell in column 2
On Error Resume Next
Set oRg = oRow.Cells(2).Range
oRg.MoveEnd wdCharacter, -1
oRg.Sort
Next oRow
End Sub

That will make the macro ignore any cells it can't sort.

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

RFJ wrote:
Hi Jay,

Tx for the response - sorry about the delay (been away)

I've got the macro installed but an error message is being generated
as follows

Runtime error "5280"
Word found no valid records to sort

Debug is highlighting the line oRg.Sort

My position on the macro learning curve is almost at the bottom so
I'm stuck as to what to do next. All I know is :
- a table is /definitely/ there
- I want to sort on Column 2 so have changed the code accordingly.

Any further help would be much appreciated.

Rob


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

First, make sure that there's a paragraph mark (not a manual line
break) at


cut





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
Can't sort table alphabetically Tiffany Microsoft Word Help 1 January 21st 05 10:17 PM
use a 'question mark cursor' to get help me Microsoft Word Help 1 January 17th 05 12:32 AM
Font question? SJH Microsoft Word Help 4 January 6th 05 10:54 AM
Column Sort BenP Microsoft Word Help 1 December 9th 04 10:15 AM
Sort Table Amy Tables 1 November 14th 04 02:28 PM


All times are GMT +1. The time now is 07:36 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"