Sorting tabular data by length

Posted on 2007-07-24
Last Modified: 2012-05-05
Hi folks

I have a mulit-column table. One of the tables contains sentences, some short and others long.

I want to sort to the data by the *length of the sentences* in a particular column.

I don't see an option when I sort so I'm assuming this calls for VBA.


Question by:Leprechaun
    LVL 92

    Expert Comment

    by:Patrick Matthews
    SELECT Sentence
    FROM Sentences
    ORDER BY LEN(Sentence)
    LVL 76

    Expert Comment

    What sort of table - database, Excel, Word?

    Author Comment

    Oh, I'm sorry. It's a table in MS Word.
    LVL 92

    Expert Comment

    by:Patrick Matthews
    Leprechaun said:
    >>Oh, I'm sorry. It's a table in MS Word.

    Rules out my suggestion, then :)
    LVL 76

    Accepted Solution

    This will do it in VBA:

    Sub SortTable()
        Dim tbl As Table
        Dim rw As Integer
        Dim s As Integer 'Sentence column
        Dim l As Integer 'last column
        s = 2
        Set tbl = ActiveDocument.Tables(1) 'for the frts table in the document
        tbl.Columns.Add 'temp column to hold sentence length
        l = tbl.Columns.Count
        For rw = 1 To tbl.Rows.Count
            tbl.Cell(rw, l).Range.Text = Len(tbl.Cell(rw, s).Range.Text)
        Next rw
        tbl.Sort FieldNumber:=l, SortFieldtype:=wdSortOrderDescending, Sortorder:=wdSortOrderAscending
        tbl.Columns.Last.Delete 'delete temp column
    End Sub

    LVL 1

    Expert Comment

    Forced accept.

    EE Admin

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now