Article ID: 136146
Article Last Modified on 10/11/2006
Sub Second_Key_Custom_Sort()
' Selects the entire list.
Selection.CurrentRegion.Select
' Does the sort here with Custom List 1 which is Normal.
' It also assumes the list has headers.
' The sort is done on column A.
Selection.Sort Key1:=Range("A1"), Header:=xlYes, OrderCustom:=1
ActiveSheet.Range("a2").Select
' Line label.
Again:
' Loops until it reachs an empty cell.
Do Until IsEmpty(ActiveCell)
' Gets row number of the starting row.
StartRow = ActiveCell.Row
' Gets the value from the starting cell.
CellVal = ActiveCell.Value
' Starts a loop that will continue until it finds a different
' value.
Do Until ActiveCell.Value <> CellVal
ActiveCell.Offset(1, 0).Select
Loop
' Selects one cell up.
ActiveCell.Offset(-1, 0).Select
' Gets the ending address.
EndAdd = ActiveCell.Address
' Gets the ending row.
EndRow = ActiveCell.Row
' Select from the beginning row to the ending row.
' If you know the columns you want to sort, you can
' concatenate them in here.
ActiveSheet.Range(StartRow & ":" & EndRow).Select
' Does the sort for the selection.
' Assumes no headers.
' Bases Sort on the 4th item in custom list.
Selection.Sort Key1:=Range("b" & StartRow), _
Header:=xlNo, OrderCustom:=4
' Selects the last cell in that group.
ActiveSheet.Range(EndAdd).Select
' Starts on the next line with a new value.
ActiveCell.Offset(1, 0).Select
' Goes to the line label again.
GoTo Again
Loop
End Sub
A1: Number B1: Month
A2: 1 B2: Sep
A3: 2 B3: Nov
A4: 1 B4: Jun
A5: 1 B5: Jan
A6: 2 B6: Mar
A7: 1 B7: Dec
A8: 2 B8: May
A9: 2 B9: Feb
A10: 1 B10: Apr
sorting data, lists
Additional query words: 7.0 XL98 XL97 XL7 XL5 custom list sorting user-defined XL
Keywords: kbhowto kbprogramming kbdtacode KB136146