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)
Syntax
'Declaration
Public ReadOnly Property Sort As Sort
public Sort Sort { get; }
Property Value
Type: Microsoft.Office.Interop.Excel.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"].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();
}
.NET Framework Security
- Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see Using Libraries from Partially Trusted Code.