Export (0) Print
Expand All
Expand Minimize

NamedRange.Sort Method

Sorts the data in a NamedRange control.

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

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

The first sort field, as either text (a range name) or a Microsoft.Office.Interop.Excel.Range object ("Dept" or Cells(1, 1), for example).

Order1
Type: Microsoft.Office.Interop.Excel.XlSortOrder

The sort order for the field or range specified in Key1.

Can be one of the following XlSortOrder values:

xlDescending. Sorts Key1 in descending order.

xlAscending. Sorts Key1 in ascending order.

Key2
Type: System.Object

The second sort field, as either text (a range name) or a Microsoft.Office.Interop.Excel.Range object. If you omit this argument, there is no second sort field.

Type
Type: System.Object

Specifies which elements are to be sorted. Use this argument only when sorting PivotTable reports.

Can be one of the following XlSortType values:

xlSortLabels. Sorts the PivotTable report by labels.

xlSortValues. Sorts the PivotTable report by values.

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

The sort order for the range specified in Key2. Cannot be used when sorting PivotTable reports.

Can be one of the following XlSortOrder values:

xlDescending. Sorts Key2 in descending order.

xlAscending. Sorts Key2 in ascending order.

Key3
Type: System.Object

The third sort field, as either text (a range name) or a Microsoft.Office.Interop.Excel.Range object. If you omit this argument, there is no third sort field. Cannot be used when sorting PivotTable reports.

Order3
Type: Microsoft.Office.Interop.Excel.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 values:

xlDescending. Sorts Key3 in descending order.

xlAscending. Sorts Key3 in ascending order.

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

Specifies whether or not the first row contains headers. Cannot be used when sorting PivotTable reports.

Can be one of the following XlYesNoGuess values:

xlGuess. Let Microsoft Office Excel determine whether there is a header, and to determine where it is, if there is one.

xlNo. (The entire range should be sorted).

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

OrderCustom
Type: System.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

true to do a case-sensitive sort; false to do a sort that is not case sensitive. Cannot be used when sorting PivotTable reports.

Orientation
Type: Microsoft.Office.Interop.Excel.XlSortOrientation

The sort orientation.

Can be one of the following XlSortOrientation values:

xlSortRows. Sorts by row.

xlSortColumns. Sorts by column.

SortMethod
Type: Microsoft.Office.Interop.Excel.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 have selected or installed.

Can be one of the following XlSortMethod values:

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

xlPinYin. Phonetic Chinese sort order for characters.

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

Specifies how to sort text in key1. Cannot be used when sorting PivotTable reports.

Can be one of the following XlSortDataOption values:

xlSortTextAsNumbers. Treat text as numeric data for the sort.

xlSortNormal. Sorts numeric and text data separately.

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

Specifies how to sort text in key2. Cannot be used when sorting PivotTable reports.

Can be one of the following XlSortDataOption values:

xlSortTextAsNumbers. Treats text as numeric data for the sort.

xlSortNormal. Sorts numeric and text data separately.

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

Specifies how to sort text in key3. Cannot be used when sorting PivotTable reports.

Can be one of the following XlSortDataOption values:

xlSortTextAsNumbers. Treats text as numeric data for the sort.

xlSortNormal. Sorts numeric and text data separately.

Return Value

Type: System.Object

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

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

If no arguments are defined with this method, Microsoft Office Excel will sort the selection in ascending order.

Optional Parameters

For information on optional parameters, see Optional Parameters in Office Solutions.

The following code example places a set of integers in cells A1 through A5 and then uses the Sort method to sort the data in ascending order.

This example is for a document-level customization.

private void SortNamedRange()
{
    // Set some data in a range of cells. 
    this.Range["A1"].Value2 = 30;
    this.Range["A2"].Value2 = 10;
    this.Range["A3"].Value2 = 20;
    this.Range["A4"].Value2 = 50;
    this.Range["A5"].Value2 = 40;

    // Create a NamedRange that refers to the range.
    Microsoft.Office.Tools.Excel.NamedRange namedRange1 =
        this.Controls.AddNamedRange(this.Range["A1", "A5"],
        "namedRange1");

    // Sort the NamedRange.
    namedRange1.Sort(this.Range["A1", "A5"],
        Excel.XlSortOrder.xlAscending,
        missing,
        missing,
        Excel.XlSortOrder.xlAscending,
        missing,
        Excel.XlSortOrder.xlAscending,
        Excel.XlYesNoGuess.xlNo,
        missing,
        missing,
        Excel.XlSortOrientation.xlSortColumns,
        Excel.XlSortMethod.xlStroke,
        Excel.XlSortDataOption.xlSortNormal,
        Excel.XlSortDataOption.xlSortNormal,
        Excel.XlSortDataOption.xlSortNormal);
}

Show:
© 2014 Microsoft