Class SortFields (Excel VBA)
The SortFields collection is a collection of SortField objects. It allows developers to store a sort state on workbooks, lists, and autofilters. To use a SortFields class variable it first needs to be instantiated, for example
Dim sfs as SortFields
Set sfs = ActiveSheet.Sort.SortFields
For Each
Here is an example of processing the SortFields items in a collection.
Dim srtfld As SortField
For Each srtfld In ActiveSheet.Sort.SortFields
Next srtfld
Add
Creates a new sort field and returns a SortFields object.
This API does not include support for data types, such as Geography or Stocks. To define a sort order based off a SubField of one of these types, see the Add2 method.
Add (Key, SortOn, Order, CustomOrder, DataOption)
Dim srtfld As SortField
Set srtfld = ActiveSheet.Sort.SortFields.Add(Key:=)
Arguments
The following argument is required
Key (Range) - Specifies a key value for the sort.
Optional arguments
The following arguments are optional
SortOn (Sort) - The field to sort on.
Order - Specifies the sort order
CustomOrder (Boolean) - Specifies if a custom sort order should be used.
DataOption - Specifies the data option
Add2
Creates a new sort field and returns a SortFields object that can optionally sort data types with the SubField defined.
This API includes support for sorting off a SubField from data types, such as Geography or Stocks. You can also use the Add method if sorting by a data type is not needed. Unlike in formulas, SubFields do not require brackets to include spaces.
Add2 (Key, SortOn, Order, CustomOrder, DataOption, SubField)
Dim srtfldAdd2 As SortField
Set srtfldAdd2 = ActiveSheet.Sort.SortFields.Add2(Key:=)
Arguments
The following argument is required
Key (Range) - Specifies a key value for the sort.
Optional arguments
The following arguments are optional
SortOn - The field to sort on
Order - Specifies the sort order
CustomOrder (Boolean) - Specifies if a custom sort order should be used.
DataOption - Specifies the data option
SubField - Specifies the field to sort on for a data type (such as Population for Geography or Volume for Stocks)
Clear
Clears all the SortFields objects.
ActiveSheet.Sort.SortFields.Clear
Count
Returns the number of objects in the collection.
Dim lngCount As Long
lngCount = ActiveSheet.Sort.SortFields.Count
Item
Returns a SortField object that represents a collection of items that can be sorted in a workbook.
Item (Index)
Index: Index value of the SortField.
Dim srtfldItem As SortField
Set srtfldItem = ActiveSheet.Sort.SortFields(Index:=1)