Sort/Order related procedures in class Table (Word VBA)

This page presents procedures from class Table related to the theme Sort/Order: Sort, SortAscending and SortDescending

Sort

Sorts the specified table.

Syntax : expression.Sort (ExcludeHeader, FieldNumber, SortFieldType, SortOrder, FieldNumber2, SortFieldType2, SortOrder2, FieldNumber3, SortFieldType3, SortOrder3, CaseSensitive, BidiSort, IgnoreThe, IgnoreKashida, IgnoreDiacritics, IgnoreHe, LanguageID)

Sub NewTableSort() 
 ActiveDocument.Tables(1).Sort ExcludeHeader:=True 
End Sub

Arguments

The following arguments are optional

ExcludeHeader (Boolean) - True to exclude the first row from the sort operation. The default value is False.

FieldNumber (Field) - The first field by which to sort. Microsoft Word sorts by FieldNumber, then by FieldNumber2, and then by FieldNumber3.

SortFieldType (WdSortFieldType) - The sort type for FieldNumber. Can be one of the WdSortFieldType constants. Some of these constants may not be available to you, depending on the language support (U.S. English, for example) that you have selected or installed. The default value is wdSortFieldAlphanumeric.

Possible Values are

wdSortFieldAlphanumeric Alphanumeric order.
wdSortFieldDate Date order.
wdSortFieldJapanJIS Japanese JIS order.
wdSortFieldKoreaKS Korean KS order.
wdSortFieldNumeric Numeric order.
wdSortFieldStroke Stroke order.
wdSortFieldSyllable Syllable order.

SortOrder (WdSortOrder) - The sorting order to use when sorting FieldNumber. Can be a WdSortOrder constant.

Possible Values are wdSortOrderAscending - Ascending order. Default, wdSortOrderDescending - Descending order.

FieldNumber2 (Integer) - The second field by which to sort.

SortFieldType2 (WdSortFieldType) - The sort type for FieldNumber2. Can be one of the WdSortFieldType constants. Some of these constants may not be available to you, depending on the language support (U.S. English, for example) that you have selected or installed. The default value is wdSortFieldAlphanumeric.

Possible Values are

wdSortFieldAlphanumeric Alphanumeric order.
wdSortFieldDate Date order.
wdSortFieldJapanJIS Japanese JIS order.
wdSortFieldKoreaKS Korean KS order.
wdSortFieldNumeric Numeric order.
wdSortFieldStroke Stroke order.
wdSortFieldSyllable Syllable order.

SortOrder2 (WdSortOrder) - The sorting order to use when sorting FieldNumber2. Can be one WdSortOrder constant.

Possible Values are wdSortOrderAscending - Ascending order. Default, wdSortOrderDescending - Descending order.

FieldNumber3 (Integer) - The third field by which to sort.

SortFieldType3 (WdSortFieldType) - The sort type for FieldNumber3. Can be one of the WdSortFieldType constants. Some of these constants may not be available to you, depending on the language support (U.S. English, for example) that you have selected or installed. The default value is wdSortFieldAlphanumeric.

Possible Values are

wdSortFieldAlphanumeric Alphanumeric order.
wdSortFieldDate Date order.
wdSortFieldJapanJIS Japanese JIS order.
wdSortFieldKoreaKS Korean KS order.
wdSortFieldNumeric Numeric order.
wdSortFieldStroke Stroke order.
wdSortFieldSyllable Syllable order.

SortOrder3 (WdSortOrder) - The sorting order to use when sorting FieldNumber3. Can be one WdSortOrder constant.

Possible Values are wdSortOrderAscending - Ascending order. Default, wdSortOrderDescending - Descending order.

CaseSensitive (Boolean) - True to sort with case sensitivity. The default value is False.

BidiSort (Boolean) - True to sort based on right-to-left language rules. This argument may not be available to you, depending on the language support (U.S. English, for example) that you have selected or installed.

IgnoreThe (Boolean) - True to ignore the Arabic character alef lam when sorting right-to-left language text. This argument may not be available to you, depending on the language support (U.S. English, for example) that you have selected or installed.

IgnoreKashida (Boolean) - True to ignore kashidas when sorting right-to-left language text. This argument may not be available to you, depending on the language support (U.S. English, for example) that you have selected or installed.

IgnoreDiacritics (Boolean) - True to ignore bidirectional control characters when sorting right-to-left language text. This argument may not be available to you, depending on the language support (U.S. English, for example) that you have selected or installed.

IgnoreHe (Boolean) - True to ignore the Hebrew character he when sorting right-to-left language text. This argument may not be available to you, depending on the language support (U.S. English, for example) that you have selected or installed.

LanguageID (WdLanguageID) - Specifies the sorting language. Can be one of the WdLanguageID constants. Refer to the Object Browser for a list of the WdLanguageID constants.

Here you can find possible values for WdLanguageID.

SortAscending

Sorts paragraphs or table rows in ascending alphanumeric order.

The first table row is considered a header record and isn't included in the sort. Use the Sort method to include the first row in a sort. This method offers a simplified form of sorting intended for mail merge data sources that contain columns of data. For most sorting tasks, use the Sort method.

If Selection.Information(wdWithInTable) = True Then 
 Selection.Tables(1).SortAscending 
Else 
 MsgBox "The insertion point is not in a table." 
End If

SortDescending

Sorts table rows in descending alphanumeric order.

The first table row is considered a header record and isn't included in the sort. Use the Sort method to include the header record in a sort. This method offers a simplified form of sorting intended for mail-merge data sources that contain columns of data. For most sorting tasks, use the Sort method.

Set newDoc = Documents.Add 
Set myTable = _ 
 newDoc.Tables.Add(Range:=Selection.Range, NumRows:=5, _ 
 NumColumns:=5) 
For iRow = 1 To myTable.Rows.Count 
 For iCol = 1 To myTable.Columns.Count 
 Set MyRange = myTable.Rows(iRow).Cells(iCol).Range 
 MyRange.InsertAfter "Cell" & Str$(iRow) & "," & Str$(iCol) 
 Next iCol 
Next iRow 
MsgBox "Click OK to sort in descending order." 
myTable.SortDescending