• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 158
  • Last Modified:

Sorting tabular data by length

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.


1 Solution
Patrick MatthewsCommented:
SELECT Sentence
FROM Sentences
ORDER BY LEN(Sentence)
What sort of table - database, Excel, Word?
LeprechaunAuthor Commented:
Oh, I'm sorry. It's a table in MS Word.
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

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

Rules out my suggestion, then :)
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

Forced accept.

EE Admin
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Tackle projects and never again get stuck behind a technical roadblock.
Join Now