Daniel,
or use
Public Sub sortOnHeaderName()
'#
'# declare private variables
'#
Dim pvt_var_CustomList(1 To 3) As Variant
'#
'# populate the array with the header names in the desired sorting order
'#
pvt_var_CustomList(1) = "Header A"
pvt_var_CustomList(2) = "Header B"
pvt_var_CustomList(3) = "Header C"
'#
'# create a custom sort list for the header names using the array as input
'#
Application.AddCustomList pvt_var_CustomList
'#
'# now sort the data horizontally using the row that contains the header names as the
'# sort argument - in this example the header names are shown in cells A1 to C1
'#
With ThisWorkbook.Worksheets("Sheet1").Sort
.SortFields.Clear
.SortFields.Add Key:=Range("A1:C1"), SortOn:=xlSortOnValues, Order:=xlAscending, _
CustomOrder:=Application.CustomListCount
.SetRange Range("A1").Resize(3, 3)
.Header = xlNo
.MatchCase = False
.Orientation = xlLeftToRight
.SortMethod = xlPinYin
.Apply
End With
'#
'# delete the last custom list added (i.e. the one we created), thus always
'# the last one existing
'#
Application.DeleteCustomList Application.CustomListCount
End Sub
Bookmarks