Home |
Search |
Today's Posts |
#1
|
|||
|
|||
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
|
|||
|
|||
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
|
|||
|
|||
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
|
|||
|
|||
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
|
|||
|
|||
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 | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Can't sort table alphabetically | Microsoft Word Help | |||
use a 'question mark cursor' to get help | Microsoft Word Help | |||
Font question? | Microsoft Word Help | |||
Column Sort | Microsoft Word Help | |||
Sort Table | Tables |