Sorts a PivotTable report, a range, or the active region (if the specified range contains only one cell).
Syntax
expression.Sort(Key1, Order1, Key2, Type, Order2, Key3, Order3, Header, OrderCustom, MatchCase, Orientation, SortMethod)
expression Required. An expression that returns a Range object.
Key1 Optional Variant. The first sort field, as either text (a PivotTable 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 constant is xlAscending.
Key2 Optional Variant. The second sort field, as either text (a PivotTable field or range name) or a Range object. If you omit this argument, there’s no second sort field. Don’t use this argument when sorting PivotTable reports.
Type Optional Variant. Specifies which elements are to be sorted. Can be one of the following XlSortType constants: xlSortLabels or xlSortValues. Use this argument only when sorting PivotTable reports.
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 constant is xlAscending. Don’t use this argument when sorting PivotTable reports.
Key3 Optional Variant. The third sort field, as either text (a range name) or a Range object. If you omit this argument, there’s no third sort field. Don’t use this argument when sorting PivotTable reports.
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 constant is xlAscending. Don’t use this argument when sorting PivotTable reports.
Header Optional Variant. Specifies whether or not the first row contains headers. Can be one of the following XlYesNoGuess constants: xlGuess, xlNo, or xlYes. 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 constant is xlNo. Don’t use this argument when sorting PivotTable reports.
OrderCustom Optional Variant. This argument is a 1-based integer offset nto the list of custom sort orders. If you omit OrderCustom, 1 (Normal) is used.
MatchCase Optional Variant. True to do a case-sensitive sort; False to do a sort that’s not case sensitive. Don’t use this argument when sorting PivotTable reports.
Orientation Optional Variant. If you use xlSortRows , the sort is done from top to bottom (by row). If you use xlSortColumns, 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: xlPinYin or xlStroke. Some of these constants may not be available to you, depending on the language support (U.S. English, for example) that you’ve selected or installed.
Remarks
The settings for Header, Order1, Order2, Order3, OrderCustom, and Orientation are saved each time you use this method. If you don’t specify values for these arguments the next time you call the method, the saved values are used. To avoid problems, set these arguments explicitly each time you use this method.