Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Sorting by Colors.

Sorting by Colors

Written by Allen Wyatt (last updated August 13, 2022)
This tip applies to Excel 97, 2000, 2002, and 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—at least not without updating to Excel 2007 or later. 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.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3122) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Sorting by Colors.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Forcing Worksheets to Print on a New Sheet

Excel allows you to easily print an entire workbook. Doing so, however, may not give the desired results if you a ...

Discover More

Transposing Table Contents

When you transpose information, it is essentially "rotated" in a direction. If you transpose the information in a table, ...

Discover More

Determining a Paragraph's Style in VBA

When processing a document via a macro, it is often helpful to understand what style has been applied to a paragraph. You ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (menu)

Sorting an Entire List

Need to sort all the data in a table? Here's the fastest and easiest way to do it.

Discover More

Sorting by Columns

When you think of sorting Excel data, it is likely that you think of sorting rows. Excel also allows you to sort by ...

Discover More

Ignoring Selected Words when Sorting

If you use Excel to maintain a list of text strings (such as movie, book, or product titles), you may want the program to ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is one less than 9?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.