
Tips.Net > ExcelTips Home > Sorting > Sorting by Colors
Summary: Excel allows you to sort by the data within a list, but it doesn’t allow you to inherently sort by cell color. This tip shows you how to overcome that shortcoming, providing a macro that makes sorting by cell color a snap. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, and Excel 2003.)
Excel allows you to sort the data in your worksheets by any number of attributes. One of the things that you cannot intrinsically sort by, however, is the color of cells. For some applications this could be a very handy feature. The following macro, SortByColor, will sort a table based on the color with which a cell is formatted.
Sub SortByColor()
On Error GoTo SortByColor_Err
Dim sRangeAddress As String
Dim sStartCell As String
Dim sEndCell As String
Dim rngSort As Range
Dim rng As Range
Application.ScreenUpdating = False
sStartCell = InputBox("Enter the cell address of the " & _
"top cell in the range to be sorted by color" & _
Chr(13) & "i.e. 'A1'", "Enter Cell Address")
If sStartCell > "" Then
sEndCell = Range(sStartCell).End(xlDown).Address
Range(sStartCell).EntireColumn.Insert
Set rngSort = Range(sStartCell, sEndCell)
For Each rng In rngSort
rng.Value = rng.Offset(0, 1).Interior.ColorIndex
Next
Range(sStartCell).Sort Key1:=Range(sStartCell), _
Order1:=xlAscending, Header:=xlNo, _
Orientation:=xlTopToBottom
Range(sStartCell).EntireColumn.Delete
End If
SortByColor_Exit:
Application.ScreenUpdating = True
Set rngSort = Nothing
Exit Sub
SortByColor_Err:
MsgBox Err.Number & ": " & Err.Description, _
vbOKOnly, "SortByColor"
Resume SortByColor_Exit
End Sub
The macro works by first asking you the beginning cell of the range you want to sort. This should be the top-most cell in the range. The macro then inserts a column (just temporarily) in which color values can be stored. It then steps through each cell in the range defined by the starting cell you specified.
SortByColor assumes your data table doesn't have a header row. If it does, you should change the actual sorting command. Simply change Header:=xlNo to Header:=xlYes.
You should note that the SortByColor macro will only sort cells based on the explicit color applied to the cell, it will not sort if the color of the cell is the result of conditional formatting.
Tip #3122 applies to Microsoft Excel versions: 97 2000 2002 2003
More Power! For some people, the prospect of creating macros can be scary. Those who conquer their fears, however, find they become much more confident and productive once they learn how to make Excel do exactly what they want. ExcelTips: The Macros is an invaluable source for learning Excel macros. You are introduced to the topic in bite-sized chunks, pulled from past issues of ExcelTips. Learn at your own pace, exactly the way you want.
Check out ExcelTips: The Macros today!
If you have tons of data to analyze, one of the best tools in Excel's arsenal is the PivotTable. Learn how to use this tool to analyze your data. (more information...)
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Beauty Tips
Bugs and Pests Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pet Tips
Word2007 Tips
WordTips
Advertise on the
ExcelTips Site