Sort Method

Applies To

Range object.

Description

Sorts a PivotTable, a range, or the current region (if the specified range contains only one cell).

Syntax

expression.Sort(Key1, Order1, Key2, Type, Order2, Key3, Order3, Header, OrderCustom,
úMatchCase, Orientation, SortMethod, IgnoreControlCharacters, IgnoreDiacritics,
úIgnoreKashida)

expression Required. An expression that returns a Range object.

Key1 Optional Variant. The first sort field, as either text (a pivot field or range name) or a Range object ("Dept" or Cells(1, 1), for example).

Order1 Optional Variant. Can be one of the following XlSortOrder constants: xlAscending or xlDescending. Use xlAscending to sort Key1 in ascending order. Use xlDescending to sort Key1 in descending order. The default value is xlAscending.

Key2 Optional Variant. The second sort field, as either text (a pivot field or range name) or a Range object. If this argument is omitted, there's no second sort field. Not used when sorting PivotTables.

Type Optional Variant. Specifies which elements are sorted. Can be one of the following XlSortType constants: xlSortValues or xlSortLabels. Used only when sorting PivotTables.

Order2 Optional Variant. Can be one of the following XlSortOrder constants: xlAscending or xlDescending. Use xlAscending to sort Key2 in ascending order. Use xlDescending to sort Key2 in descending order. The default value is xlAscending. Not used when sorting PivotTables.

Key3 Optional Variant. The third sort field, as either text (a range name) or a Range object. If this argument is omitted, there's no third sort field. Not used when sorting PivotTables.

Order3 Optional Variant. Can be one of the following XlSortOrder constants: xlAscending or xlDescending. Use xlAscending to sort Key3 in ascending order. Use xlDescending to sort Key3 in descending order. The default value is xlAscending. Not used when sorting PivotTables.

Header Optional Variant. Specifies whether the first row contains headers. Can be one of the following XlYesNoGuess constants: xlYes, xlNo, or xlGuess. Use xlYes if the first row contains headers (it shouldn't be sorted). Use xlNo if there are no headers (the entire range should be sorted). Use xlGuess to let Microsoft Excel determine whether there's a header, and to determine where it is, if there is one. The default value is xlNo. Not used when sorting PivotTables.

OrderCustom Optional Variant. A 1-based integer offset into the list of custom sort orders. If this argument is omitted, 1 (Normal) is used.

MatchCase Optional Variant. True to do a case-sensitive sort; False to do a sort that's not case sensitive. Not used when sorting PivotTables.

Orientation Optional Variant. If xlTopToBottom is used, the sort is done from top to bottom (by row). If xlLeftToRight is used, the sort is done from left to right (by column).

SortMethod Optional Variant. The type of sort. Can be one of the following XlSortMethod constants: xlSyllabary (to sort phonetically) or xlCodePage (to sort by code page). The default value is xlSyllabary.

IgnoreControlCharacters Optional Variant. Not used in US/English Microsoft Excel.

IgnoreDiacritics Optional Variant. Not used in US/English Microsoft Excel.

IgnoreKashida Optional Variant. Not used in US/English Microsoft Excel.

See Also

SortSpecial method.

Example

This example sorts the range A1:C20 on Sheet1, using cell A1 as the first sort key and cell B1 as the second sort key. The sort is done in ascending order by row, and there are no headers.

Worksheets("Sheet1").Range("A1:C20").Sort _
    Key1:=Worksheets("Sheet1").Range("A1"), _
    Key2:=Worksheets("Sheet1").Range("B1")
This example sorts the current region that contains cell A1 on Sheet1, sorting by the data in the first column and automatically using a header row if one exists. The Sort method determines the current region automatically.

Worksheets("Sheet1").Range("A1").Sort _
    Key1:=Worksheets("Sheet1").Columns("A"), _
    Header:=xlGuess