다음을 통해 공유


WorksheetBase.Sort Property

Definition

Gets the sorted values in the current worksheet.

public:
 property Microsoft::Office::Interop::Excel::Sort ^ Sort { Microsoft::Office::Interop::Excel::Sort ^ get(); };
public Microsoft.Office.Interop.Excel.Sort Sort { get; }
member this.Sort : Microsoft.Office.Interop.Excel.Sort
Public ReadOnly Property Sort As Sort

Property Value

The sorted values in the current worksheet.

Examples

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();
}
Private Sub SortWorksheet()        
    ' Populate worksheet with some data
    Me.Range("A1").Value2 = "First Name"
    Me.Range("B1").Value2 = "Last Name"
    Me.Range("A2").Value2 = "Valery"
    Me.Range("B2").Value2 = "Ushakov"
    Me.Range("A3").Value2 = "Rachel"
    Me.Range("B3").Value2 = "Valdez"

    ' Set sort properties        
    Me.Sort.SetRange(Me.Range("A1", "B3"))
    Me.Sort.Header = Excel.XlYesNoGuess.xlYes        
    Me.Sort.SortFields.Add(Me.Range("A1", "A3"), _
        Excel.XlSortOn.xlSortOnValues, _
        Excel.XlSortOrder.xlAscending)

    ' Sort worksheet
    Me.Sort.Apply()
End Sub

Applies to