Partager via


Range.Sort Method

Sorts a PivotTable report, a range, or the active region if the specified range contains only one cell.

Namespace:  Microsoft.Office.Interop.Excel
Assembly:  Microsoft.Office.Interop.Excel (in Microsoft.Office.Interop.Excel.dll)

Syntax

'Declaration
Function Sort ( _
    Key1 As Object, _
    Order1 As XlSortOrder, _
    Key2 As Object, _
    Type As Object, _
    Order2 As XlSortOrder, _
    Key3 As Object, _
    Order3 As XlSortOrder, _
    Header As XlYesNoGuess, _
    OrderCustom As Object, _
    MatchCase As Object, _
    Orientation As XlSortOrientation, _
    SortMethod As XlSortMethod, _
    DataOption1 As XlSortDataOption, _
    DataOption2 As XlSortDataOption, _
    DataOption3 As XlSortDataOption _
) As Object
'Usage
Dim instance As Range
Dim Key1 As Object
Dim Order1 As XlSortOrder
Dim Key2 As Object
Dim Type As Object
Dim Order2 As XlSortOrder
Dim Key3 As Object
Dim Order3 As XlSortOrder
Dim Header As XlYesNoGuess
Dim OrderCustom As Object
Dim MatchCase As Object
Dim Orientation As XlSortOrientation
Dim SortMethod As XlSortMethod
Dim DataOption1 As XlSortDataOption
Dim DataOption2 As XlSortDataOption
Dim DataOption3 As XlSortDataOption
Dim returnValue As Object

returnValue = instance.Sort(Key1, Order1, _
    Key2, Type, Order2, Key3, Order3, Header, _
    OrderCustom, MatchCase, Orientation, _
    SortMethod, DataOption1, DataOption2, _
    DataOption3)
Object Sort(
    Object Key1,
    XlSortOrder Order1,
    Object Key2,
    Object Type,
    XlSortOrder Order2,
    Object Key3,
    XlSortOrder Order3,
    XlYesNoGuess Header,
    Object OrderCustom,
    Object MatchCase,
    XlSortOrientation Orientation,
    XlSortMethod SortMethod,
    XlSortDataOption DataOption1,
    XlSortDataOption DataOption2,
    XlSortDataOption DataOption3
)

Parameters

  • Key1
    Type: System.Object

    Optional Object. 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
    Type: Microsoft.Office.Interop.Excel.XlSortOrder

    Optional XlSortOrder. The sort order for the field or range specified in Key1. Can be one of the following XlSortOrder constants:

    • xlDescending. Sorts Key1 in descending order.

    • xlAscendingdefault. Sorts Key1 in ascending order.

  • Key2
    Type: System.Object

    Optional Object. 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. Cannot be used when sorting Pivot Table reports.

  • Type
    Type: System.Object

    Optional Object. Specifies which elements are to be sorted. Use this argument only when sorting PivotTable reports. Can be one of the following XlSortType constants:

    • xlSortLabels. Sorts the PivotTable report by labels.

    • xlSortValues. Sorts the PivotTable report by values.

  • Order2
    Type: Microsoft.Office.Interop.Excel.XlSortOrder

    Optional XlSortOrder. The sort order for the field or range specified in Key2. Cannot be used when sorting PivotTable reports. Can be one of the following XlSortOrder constants:

    • xlDescending. Sorts Key2 in descending order.

    • xlAscendingdefault. Sorts Key2 in ascending order.

  • Key3
    Type: System.Object

    Optional Object. 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. Cannot be used when sorting PivotTable reports.

  • Order3
    Type: Microsoft.Office.Interop.Excel.XlSortOrder

    Optional XlSortOrder. The sort order for the field or range specified in Key3. Cannot be used when sorting PivotTable reports. Can be one of the following XlSortOrder constants:

    • xlDescending. Sorts Key3 in descending order.

    • xlAscendingdefault. Sorts Key3 in ascending order.

  • Header
    Type: Microsoft.Office.Interop.Excel.XlYesNoGuess

    Optional XlYesNoGuess. Specifies whether or not the first row contains headers. Cannot be used when sorting PivotTable reports. Can be one of the following XlYesNoGuess constants:

    • xlGuess. Let Microsoft Excel determine whether there’s a header, and determine where it is if there is one.

    • xlNodefault. (The entire range should be sorted).

    • xlYes. (The entire range should not be sorted).

  • OrderCustom
    Type: System.Object

    Optional Object. This argument is a one-based integer offset to the list of custom sort orders. If you omit OrderCustom, a normal sort is used.

  • MatchCase
    Type: System.Object

    Optional Object. True to do a case-sensitive sort; False to do a sort that’s not case sensitive. Cannot be used when sorting PivotTable reports.

  • SortMethod
    Type: Microsoft.Office.Interop.Excel.XlSortMethod

    Optional XlSortMethod. The type of sort. 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. Can be one of the following XlSortMethod constants:

    • xlStroke Sorting by the quantity of strokes in each character.

    • xlPinYindefault. Phonetic Chinese sort order for characters.

  • DataOption1
    Type: Microsoft.Office.Interop.Excel.XlSortDataOption

    Optional XlSortDataOption. Specifies how to sort text in Key 1. Cannot be used when sorting PivotTable reports. Can be one of the following XlSortDataOption constants:

    • xlSortTextAsNumbers. Treat text as numeric data for the sort.

    • xlSortNormaldefault. Sorts numeric and text data separately.

  • DataOption2
    Type: Microsoft.Office.Interop.Excel.XlSortDataOption

    Optional XlSortDataOption. Specifies how to sort text in Key 2. Cannot be used when sorting PivotTable reports. Can be one of the following XlSortDataOption constants:

    • xlSortTextAsNumbers. Treats text as numeric data for the sort.

    • xlSortNormaldefault. Sorts numeric and text data separately.

  • DataOption3
    Type: Microsoft.Office.Interop.Excel.XlSortDataOption

    Optional XlSortDataOption. Specifies how to sort text in key 3. Cannot be used when sorting PivotTable reports. Can be one of the following XlSortDataOption constants:

    • xlSortTextAsNumbers. Treats text as numeric data for the sort.

    • xlSortNormaldefault. Sorts numeric and text data separately.

Return Value

Type: System.Object

Remarks

The settings for Header, Order1, Order2, Order3, OrderCustom, and Orientation are saved, for the particular worksheet, 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. Set these arguments explicitly each time you use Sort method if you choose not to use the saved values.

Text strings which are not convertible to numeric data are sorted normally.

If no arguments are defined with the Sort method, Microsoft Excel will sort the selection, chosen to be sorted, in ascending order.

See Also

Reference

Range Interface

Range Members

Microsoft.Office.Interop.Excel Namespace