Loading
Excel.Tips.Net ExcelTips (Menu Interface)

Sorting by Fill Color

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.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2009) applies to Microsoft Excel 97, 2000, 2002, and 2003.

Related Tips:

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

 

Leave your own comment:

*Name:
Email:
  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*Text:
*What is 5+3 (To prevent automated submissions and spam.)
 
 
           Commenting Terms

Comments for this tip:

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

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us

 

Advertise with Us

Our Privacy Policy

Our Sites

Tips.Net

Beauty and Style

Cars

Cleaning

Cooking

DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)

Gardening

Health

Home Improvement

Money and Finances

Organizing

Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Our Products

Helpful E-books

Newsletter Archives

 

Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2016 Sharon Parq Associates, Inc.