Setting Cell Color Based on Numeric Values

Written by Allen Wyatt (last updated January 9, 2021)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and 2021


1

Joseph uses regression to determine the result of color blending. He would love for a cell to be colored based on the RGB or HEX value data contained in a cell, but knows there is no way to do this. So, he ends up coloring cells manually. Most of his worksheets have many hundreds of colors, so this is very tedious. Joseph wonders if there is a way to automatically color a cell based on the RGB or HEX values contained in another cell.

Yes, this can be done using a macro. More appropriately, it can be done using an event handler which is a special type of macro that is run automatically based on an "event" that occurs within Excel. In this case you can create an event handler that is executed anytime something is changed in your worksheet, like this:

Private Sub Worksheet_Change(ByVal Target As Range)
    Range(A2).Interior.Color = CLng("&H" & Range("A1").Value)
End Sub

This macro should be placed in the code for the worksheet you want to affect, so right-click on the worksheet's tab and choose View Code from the resulting Context menu. You can then paste the code into the Code window you are shown.

This particular macro assumes that the desired hex value is in cell A1 and then changes the color in A2 based on that hex value. Using the macro will slow down your use of Excel a bit, as it is executed every single time something changes in the worksheet. If you prefer to change colors just by manually running the macro, you could use this variation, instead:

Sub SetColors()
    Dim rSource As Range
    Dim rTarget As Range
    Dim J As Integer

    Set rSource = Range("A1:A50")
    Set rTarget = Range("D1:D50")

    For J = 1 To rSource.Cells.Count
        rTarget.Cells(J).Interior.Color = CLng("&H" & rSource.Cells(J).Value)
    Next J
End Sub

When you run this macro, it looks at the range assigned to the rSource variable (A1:A50) and uses the values in that range to set the colors in the range assigned to the rTarget variable (D1:D50). Assuming you change the ranges assigned to both rSource and rTarget, you should make sure that both ranges contain the same number of cells.

So far the macros in this tip have relied on the source cell containing a hex value for the color desired. If you would rather work with individual RGB values for the cells, that has been covered in a different tip that you may find helpful.

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 (9963) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and 2021.

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

Defining a Shortcut for a Macro

You can make running macros very easy if you assign a shortcut key to the macro. This tip demonstrates how easy it is to ...

Discover More

Using AutoSave in Excel

It is a good precaution to periodically save your work. That way, if you have an unforeseen problem with your computer, ...

Discover More

Notification when Recalculation is Done

If you manually recalculate your workbooks, you are probably doing so because of the time it takes to do the ...

Discover More

Dive Deep into Macros! Make Excel do things you thought were impossible, discover techniques you won't find anywhere else, and create powerful automated reports. Bill Jelen and Tracy Syrstad help you instantly visualize information to make it actionable. You’ll find step-by-step instructions, real-world case studies, and 50 workbooks packed with examples and solutions. Check out Microsoft Excel 2019 VBA and Macros today!

More ExcelTips (ribbon)

Accurate Font Sizes

Need to use some bizarre font size in your worksheet? Not a problem, provided it is a full or half point size.

Discover More

Leaving Leading Zeros in Place

There are some numbers that require leading zeros, such as ZIP Codes. Excel provides several different ways that you can ...

Discover More

Removing All Formatting

Getting rid of formatting from a cell or group of cells can be done using several different techniques. This tip ...

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 seven less than 7?

2021-01-09 17:24:58

David Gray

What's wrong with conditional formatting? I'd save the VBA for when I really need it. I apply conditional formats to accomplish this almost daily. As a bonus, such conditional formatting can easily be extended to the row or column in which the value appears.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu 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.