SortFields.Add method (Excel)

Creates a new sort field and returns a SortFields object.

Syntax

expression.Add (Key, SortOn, Order, CustomOrder, DataOption)

expression A variable that represents a SortFields object.

Parameters

Name Required/Optional Data type Description
Key Required Range Specifies a key value for the sort.
SortOn Optional Variant An XlSortOn value that specifies which property of a cell to use for the sort.
Order Optional Variant An XlSortOrder value that specifies the sort order.
CustomOrder Optional Variant Specifies if a custom sort order should be used.
DataOption Optional Variant An XlSortDataOption value that specifies how to sort text.

Return value

SortField

Remarks

This API does not include support for data types, such as Geography or Stocks. To define a sort order based off a SubField of one of these types, see the Add2 method.

Example

This example sorts a table, Table1 on Sheet1, by Column1 in ascending order.

The Clear method is called before to ensure that the previous sort is cleared so that a new one can be applied.

The Sort object is called to apply the added sort to Table1.

ActiveWorkbook.Worksheets("Sheet1").ListObjects("Table1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").ListObjects("Table1").Sort.SortFields.Add _
 Key:=Range("Table1[[#All],[Column1]]"), _
 SortOn:=xlSortOnValues, _
 Order:=xlAscending, _
 DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").ListObjects("Table1").Sort
 .Header = xlYes
 .MatchCase = False
 .Orientation = xlTopToBottom
 .SortMethod = xlPinYin
 .Apply
End With

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.