Sorting by Fill Color

Written by Allen Wyatt (last updated September 13, 2022)
This tip applies to Excel 97, 2000, 2002, and 2003


Chuck wrote about a need he has to sort records in a worksheet based on the fill color used in a cell. Excel provides no intrinsic function to perform such an action, but it is possible to create a user-defined function that will help with any sorting that needs to be done. Consider the following macro:

Function GetFillColor(rng As Range) As Long
    GetFillColor = rng.Interior.ColorIndex
End Function

Assuming the fill colors are in the cells of column A, all you need to do is make sure there is an empty column B. Then place the following formula in cell B2 and copy it down for each record:

=GetFillColor(A2)

When you are done, column B will contain the index values of each fill color used in column A. You can then sort by column B, which has the result of grouping all the like fill colors together.

If you need to get more elaborate, for instance, if you need to sort in a particular order (yellow first, red second, green third, etc.), then you cannot rely solely on the fill color's index value. In such an instance you must rely on a different method of returning a color. Consider the following macro:

Function GetColor(rngIndex As Range, rngSource As Range) As Long
    Dim lngColor As Long
    Dim J As Integer

    Application.Volatile
    lngColor = rngSource.Interior.ColorIndex

    GetColor = 99       'Set to default color
    For J = 1 To rngIndex.Count
        If rngIndex(J).Interior.ColorIndex = lngColor Then
            GetColor = J
        End If
    Next J
End Function

This macro works differently than the last one. It requires two ranges to work properly. The first range is basically a color table which indicates the order in which you want colors sorted. For instance, cells E1 through E9 could contain the nine colors you want to use for sorting, in the order that you want them sorted. You would then place the following formula in cell B2 and copy it down for each record:

=GetColor($E$1:$E$9,A2)

The result is that column B will contain the values 1 through 9, representing the colors in your color table. If the color in a cell does not have a corresponding color in the color table, then the function returns the value of 99. When you sort the records in your table, you end up with them sorted as you want.

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 (2009) applies to Microsoft Excel 97, 2000, 2002, and 2003.

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

Accessing a Problem Shared Workbook

What are you to do is you share a workbook with others, and then suddenly the workbook won't open properly? Dealing with ...

Discover More

Moving a Chart's Legend

Need to move a chart legend to a different place on the chart? It's easy to do using the mouse, as described in this tip.

Discover More

File Formats that Include Field Formats

If you import data into Excel that is created by other programs, you know that it can be bothersome to get your data ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (menu)

Counting Colors of Cells

Many people use colors of cells as a common method of communicating information in a worksheet. If you need a way to ...

Discover More

Generating Unique Sequential Numbers

Using Excel to generate unique sequential numbers for invoices or company statements can be a challenge. Here's ...

Discover More

Determining Combinations to Make a Total

If you have a range of cells that contain values, you may wonder which combinations of those cells should be used to meet ...

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 nine minus 5?

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.