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.
Method 1: Use Word VBA
For example, let’s say we have a table as bellow:
- First and foremost, in Word press “Alt+ F11” to trigger VBA editor.
- Go to create a new module by clicking “Normal” in the left column.
- Then click “Insert” tab and choose “Module” on the drop-down menu.
- 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”.
- Next click “Run”.
- Then in the first box open, enter the column number of target column, such as “2” for the second column.
- Click “OK”.
- In the second box, enter a number to specify a sorting rule. “0” represents sorting by ascending, “1” by descending.
- Similarly, click “OK”.
Check the effect:
Method 2: Sort in Excel
- Firstly, select table in Word and press “Ctrl+ C” to copy it.
- 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.
- 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.
- Next right click and choose “Insert”.
- There will be a new column before column C. Position cursor at the second cell of new column.
- Enter “=LEN(C2)” in cell B2. Remember to replace the “C” with an actual one.
- Next put cursor at the right-down corner of cell B2 until it changes to a cross.
- Now double click. You will see the new column filled with numbers. These are number of characters for the texts in column C.
- Enter anything you like in cell B1. Just don’t leave it blank.
- Put cursor in any cell in column B that has a number.
- Go to click “Home” tab and click “Sort & Filter”.
- On the drop-down menu, choose “Sort Smallest to Largest” or “Sort Largest to Smallest”.
- 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