Sort Property

WorksheetBase.Sort Property

 

Gets the sorted values in the current worksheet.

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

public Sort Sort { get; }

Property Value

Type: Microsoft.Office.Interop.Excel.Sort

The sorted values in the current worksheet.

The following code example fills a range in the current worksheet with a header and employee names. Next, the example accesses the Sort property of the worksheet and sets properties that include the column range to sort by and the sorting order. Finally, the code calls the Microsoft.Office.Interop.Excel.Sort.Apply method to sort the specified worksheet data. When you run this code, the employee data will be sorted in ascending order based on the employee first name.

This example is for a document-level customization.

private void SortWorksheet()  
{
    // Populate worksheet with some data
    this.Range["A1"].Value2 = "First Name";
    this.Range["B1"].Value2 = "Last Name";
    this.Range["A2"].Value2 = "Valery";
    this.Range["B2"].Value2 = "Ushakov";
    this.Range["A3"].Value2 = "Rachel";
    this.Range["B3"].Value2 = "Valdez";

    // Set sort properties            
    this.Sort.SetRange(this.Range["A1", "B3"]);
    this.Sort.Header = Excel.XlYesNoGuess.xlYes;
    this.Sort.SortFields.Add(this.Range["A1", "A3"], Excel.XlSortOn.xlSortOnValues,
        Excel.XlSortOrder.xlAscending);

    // Sort worksheet
    this.Sort.Apply();
}
Return to top
Show:
© 2016 Microsoft