Sort Method

This method sorts a range or the current region when the specified range contains only one cell. It can also be used to sort a pivot table. The syntax is:

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

Sorting can take place based on up to three keys, denoted by Key1, Key2, and Key3. These parameters can be expressed as text (a range name) or a Range object. The corresponding Order parameter can be set to one of the following values:

Enum XlSortOrder xlAscending = 1 ' Default xlDescending = 2 End Enum

The optional Type parameter is used only when sorting pivot tables.

The optional Header parameter specifies whether the first row contains headers, in which case they are not included in the sort. The Header parameter can be one of the following values:

Enum XlYesNoGuess xlGuess = 0 xlYes = 1

End Enum

The optional OrderCustom parameter is an integer offset into the list of custom sort orders. However, Microsoft seems not to have documented this further, so it seems best to simply omit this argument, in which case it is assumed to be Normal (which sounds good).

The optional MatchCase parameter should be set to True to do a case-sensitive sort and False (the default) to do a sort that is not case-sensitive. For instance, suppose that cell A1 contains the text "AAA" and cell A2 contains the text "aaa." The code:

Range("A1:A2").Sort Key1:=Cells(1, 1), MatchCase:=True will swap the contents of these two cells, but the code:

Range("A1:A2").Sort Key1:=Cells(1, 1), MatchCase:=False will not.

The optional Orientation parameter determines whether the sort is done by row or by column. It can assume either of the values in the following enum:

Enum XlSortOrientation xlSortColumns = 1 xlSortRows = 2 End Enum

For instance:

Range("A1:B2").Sort Key1:=Rows(1), Orientation:=xlSortColumns sorts the columns in the range A1:B2 using the first row for the sort key.

The rest of the parameters are not used in the U.S. English version of Excel. The SortMethod parameter is not documented, but it has a default value xlPinYin, whatever that means.

0 0

Post a comment