Results 1 to 8 of 8
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Sort Table by Cells (XP)

    Unless I've missed something, there's no way to sort a table by cells; only by one or more columns.

    My annual Christmas wish-list is a 3-column 12-row table. Into the cells I jot down Things-I'd-Like-To-Get-Done over the break.

    "Resite desk etc", "Fix send-to-spare problem", "Ebooks", and so on.

    I'd like to arrange the data so that the top left-hand cell had the lowest-sequence entry, and that cell contents were arranged in sequence moving to the right, until the end of the first row was reached, then continuing with the leftmost cell of the second row and so on.

    The VBA code to do this is trivial - load all cells to an array, ignring empty cells, sort the array, and write it back to the table.

    I'd rather not do that if I've just overlooked an option in the Table, Sort menu.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: Sort Table by Cells (XP)

    There is no built-in feature to sort a table the way you want to, so I think you'll have to roll your own code.

  3. #3
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Stocking stuffer

    > you'll have to roll your own code.

    OK (sigh). Guess I'd better add it to the Wish List.

    (later) Here it is (attached)
    Attached Files Attached Files

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: Stocking stuffer

    Thanks for sharing. You can shorten the macro a bit by removing the fluff inserted by the macro recorder:

    <code>Sub SortCellsAscending()
    '
    ' Macro1 Macro
    ' Macro recorded 12/17/2004 by Chris Greaves
    ' Macro modified 12/17/2004 by Chris Greaves
    '
    If Selection.Information(wdWithInTable) Then
    ''' Grab the table
    Selection.Tables(1).Select
    ''' Remember the number of columns for rebuilding later on
    Dim lngColumns As Long
    lngColumns = Selection.Tables(1).Columns.Count
    ''' Convert to serial paragraphs
    Selection.Rows.ConvertToText Separator:=wdSeparateByParagraphs
    ''' Sort the paragraphs
    Selection.Sort
    ''' Convert back to the recorded number of Columns
    Selection.ConvertToTable Separator:=wdSeparateByParagraphs, _
    NumColumns:=lngColumns
    Else
    MsgBox "Your selection point must be in a table for this to work!"
    End If
    End Sub</code>

  5. #5
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Stocking stuffer

    Although certainly not any faster, (especially since you've already done it) <img src=/S/grin.gif border=0 alt=grin width=15 height=15>, you could have:
    1. Converted the table to text. (Table/Convert/Table to text...)
    2. Replaced the tabs with paragraph marks.
    3. Sorted the text by paragraph.
    4. Copied them.
    5. Create the new table & select the cells.
    6. Paste into the selected cells. Word will paste across the row & then continue on the next row.

    Sounds more involved than it is.

  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Stocking stuffer

    > You can shorten the macro a bit

    I can shorten it even more:

    <pre>Call U.TableSortByCells(tbl, blnSequence, blnCaseSensitive)
    </pre>


    (grin!)

  7. #7
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Stocking stuffer

    > Sounds more involved than it is.

    Not really. I think it looks about the same as the method I used. You are suggesting a "Paste" technique for writing data by row, and I am using a 'Convert" technique to write by rows.

    Apart from that, aren't we following the same path?

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: Stocking stuffer

    Cheat!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •