Sorting by Color


Posted by Kierrica Firson on July 25, 2001 9:12 AM

Is there a way to sort rows by the color? If I have highlighted various rows throughout a large set of data, can I make it so all the highlighted rows appear at the top so I don't have to scroll down and search for every one of them?



Posted by faster on July 25, 2001 11:35 AM

Sub SortColors()
'test this on a copy of your data first because
'you can't undo it
'code only sorts highlighted or not highlighted
'insert a column in your table and title it SORT
'select the cell in the second row of the SORT column

Dim Anchor
Anchor = ActiveCell.Address

If MsgBox("Sort by color", vbYesNo) = vbNo Then
Exit Sub
End If

Do While Selection.Row <> ActiveCell.SpecialCells(xlLastCell).Row
If Selection.Interior.ColorIndex <> xlNone Then
Selection = "X"
Selection.Offset(1, 0).Select
Else
Selection.Offset(1, 0).Select
End If
Loop

Range(Anchor).Select

Selection.Sort _
Key1:=Range(Anchor), _
Order1:=xlAscending, _
Header:=xlYes, _
OrderCustom:=1, _
MatchCase:=False, _
Orientation:=xlTopToBottom

End Sub