Share via


Worksheet.Sort Property (2007 System)

Gets the sorted values in the current worksheet.

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

Syntax

'Declaration
<BrowsableAttribute(False)> _
Public ReadOnly Property Sort As Sort
'Usage
Dim instance As Worksheet 
Dim value As Sort 

value = instance.Sort
[BrowsableAttribute(false)]
public Sort Sort { get; }
[BrowsableAttribute(false)]
public:
property Sort^ Sort {
    Sort^ get ();
}
public function get Sort () : Sort

Property Value

Type: Sort
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 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
private void SortWorksheet()  
{
    // Populate worksheet with some data 
    this.Range["A1",missing].Value2 = "First Name";
    this.Range["B1",missing].Value2 = "Last Name";
    this.Range["A2",missing].Value2 = "Valery";
    this.Range["B2",missing].Value2 = "Ushakov";
    this.Range["A3",missing].Value2 = "Rachel";
    this.Range["B3",missing].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, missing, missing);

    // Sort worksheet 
    this.Sort.Apply();
}

.NET Framework Security

See Also

Reference

Worksheet Class

Worksheet Members

Microsoft.Office.Tools.Excel Namespace