2 Smart Ways to Sort a Column of Texts by Length in Your Word

In today’s article, we will show you 2 smart ways to sort a column of texts by length in your Word.

There are several ways to sort a column of texts or numbers in a table. But, have you ever thought about sorting a column of texts by length? In such case, your document layout will be more clear and easy for reading.

However, there is no direct way in Word for us to sort in this rule. Therefore, we have to look for workarounds to solve the issue.Sort a Column of Texts by Length

Method 1: Use Word VBA

For example, let’s say we have a table as bellow:Original Table

  1. First and foremost, in Word press “Alt+ F11” to trigger VBA editor.
  2. Go to create a new module by clicking “Normal” in the left column.
  3. Then click “Insert” tab and choose “Module” on the drop-down menu.Click "Normal"->Click "Insert"->Click "Module"
  4. Next double click on module to open it and paste the bellowing macro there:
Sub SortByWordLength()
  Dim objTable As Table
  Dim objColumnCell As Cell
  Dim objColumnCellRange As Range
  Dim objNewColumnCellRange As Range
  Dim nRowNumber As Integer
  Dim nColumnNumber As Integer
  Dim strWordLenth As String
  Dim nSortOrder As Integer
  Dim nCurrentTableIndex As Integer
  Dim nTableColumnsInCurrentTable As Integer
 
  nCurrentTableIndex = ActiveDocument.Range(0, Selection.Tables(1).Range.End).Tables.Count
  nTableColumns = ActiveDocument.Tables(nCurrentTableIndex).Columns.Count

  nColumnNumber = InputBox("Enter the column number you want to sort", "Column Number", "For example:2")
 
  If nColumnNumber > 0 And nColumnNumber <= nTableColumns Then
    nSortOrder = InputBox("Choose the sort order:" & vbNewLine & "If you want to sort by descending, click 1" & vbNewLine & "If you want to sort by ascending, click 0", "Sort Order", "For example:1")

    If nSortOrder = 1 Or nSortOrder = 0 Then
      '  Add a new column to put the word length of the specified column. 
      Set objTable = ActiveDocument.Tables(nCurrentTableIndex)
      objTable.Columns.Add BeforeColumn:=objTable.Columns(nColumnNumber)
      nRowNumber = 1

      For Each objColumnCell In objTable.Columns(nColumnNumber + 1).Cells
        Set objColumnCellRange = objColumnCell.Range
        objColumnCellRange.MoveEnd Unit:=wdCharacter, Count:=-1
        Set objNewColumnCellRange = objTable.Cell(nRowNumber, nColumnNumber).Range
        objNewColumnCellRange.MoveEnd Unit:=wdCharacter, Count:=-1
 
        strWordLenth = Len(objColumnCellRange.Text)
 
        objNewColumnCellRange.InsertAfter (strWordLenth)
 
        nRowNumber = nRowNumber + 1
      Next objColumnCell
 
      objTable.Select
 
      '  Sort by the word length. 
      Selection.Sort ExcludeHeader:=True, FieldNumber:="Column " & nColumnNumber, SortFieldType:= _
        wdSortFieldNumeric, SortOrder:=nSortOrder, FieldNumber2:="", _
        SortFieldType2:=wdSortFieldAlphanumeric, SortOrder2:=wdSortOrderAscending _
      ,  FieldNumber3:="", SortFieldType3:=wdSortFieldAlphanumeric, SortOrder3:= _
        wdSortOrderAscending, Separator:=wdSortSeparateByCommas, SortColumn:= _
        False, CaseSensitive:=False, LanguageID:=wdEnglishUS, SubFieldNumber:= _
        "Paragraphs", SubFieldNumber2:="Paragraphs", SubFieldNumber3:="Paragraphs"
 
      objTable.Columns(nColumnNumber).Delete
 
    Else
      MsgBox ("Invalid sort type, please try again")
    End If
  Else
    MsgBox ("Invalid column number, please try again")
  End If
End Sub

Note:

If your table contains no header row, then you need to alter the following code line:

Selection.Sort ExcludeHeader:=True

Replace “True” with “False”.

  1. Next click “Run”.Paste Codes->Click "Run"
  2. Then in the first box open, enter the column number of target column, such as “2” for the second column.
  3. Click “OK”.Enter Column Number->Click "OK"
  4. In the second box, enter a number to specify a sorting rule. “0” represents sorting by ascending, “1” by descending.
  5. Similarly, click “OK”.Enter a Number to Specify a Sorting Rule->Click "OK"

Check the effect:Effect

Method 2: Sort in Excel

  1. Firstly, select table in Word and press “Ctrl+ C” to copy it.
  2. Next open Excel and paste it by pressing “Ctrl+ V”. Now if your table has no header row, then paste the table in cell A2.
  3. Then click on the column letter of the column. For instance, let’s say column C is the target column. You will see the total column in selection.
  4. Next right click and choose “Insert”.Right Click ->Choose "Insert"
  5. There will be a new column before column C. Position cursor at the second cell of new column.
  6. Enter “=LEN(C2)” in cell B2. Remember to replace the “C” with an actual one.
  7. Next put cursor at the right-down corner of cell B2 until it changes to a cross.
  8. Now double click. You will see the new column filled with numbers. These are number of characters for the texts in column C.New Column with Character Numbers
  9. Enter anything you like in cell B1. Just don’t leave it blank.
  10. Put cursor in any cell in column B that has a number.
  11. Go to click “Home” tab and click “Sort & Filter”.
  12. On the drop-down menu, choose “Sort Smallest to Largest” or “Sort Largest to Smallest”.Click "Home"->Click "Sort &Filter"->Choose a Sorting Rule
  13. Finally, select the newly inserted column and right click to choose “Delete”. And paste the table back to Word document.

Repair Corrupt Documents

Do you believe your documents stored on computers are safe? Even you can protect your computer from attack. There is still the possibility of having documents lost. So keep in mind that your file might not be perfectly safe. Consequently, get a docx recovery tool in advance would be a good choice.

Author Introduction:

Vera Chen is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including damaged xls and pdf repair software products. For more information visit www.datanumen.com

Leave a Reply

Your email address will not be published. Required fields are marked *