Dynamically Changing Worksheet Tab Color

Written by Allen Wyatt (last updated July 13, 2019)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


4

Andrew knows how to change the color of worksheet tabs manually. However, he would like a way to change the tab color based upon a value in a cell on the worksheet.

To do this, you'll need to use a macro. The key is that you want to change the Color property of the Tab object, in this manner:

ActiveSheet.Tab.Color = vbRed

The logic you use to get to the point of making such a color assignment depends on what you want to do and when you want to do it. Assuming that you want to change the tab color based on what is in cell A1, you could use a macro like the following:

Private Sub Worksheet_Change(ByVal Target As Range)
    MyVal = Range("A1").Text

    With ActiveSheet.Tab
        Select Case MyVal
            Case "0"
                .Color = vbBlack
            Case "1"
                .Color = vbRed
            Case "2"
                .Color = vbGreen
            Case "3"
                .Color = vbYellow
            Case "4"
                .Color = vbBlue
            Case "5"
                .Color = vbMagenta
            Case "6"
                .Color = vbCyan
            Case "7"
                .Color = vbWhite
            Case Else
                .ColorIndex = xlColorIndexNone
        End Select
    End With
End Sub

You need to add the macro to the code for the worksheet whose tab you want to modify. (Right-click the sheet's tab and choose View Code from the Context menu. Paste the code into that code window.) The macro grabs whatever is in cell A1 and then uses a Select Case structure to change the color of the tab. The logic changes the color if A1 contains 0 through 7. If there is anything else there (or nothing at all), then the ColorIndex property is used to set the tab color back to its default.

The macro could be modified so that what it tests for is a text string (such as "Black", "Red", etc.) or some keyword (such as "Low" or "High"). You could also use different color designations with the Color property, such as the RGB function:

                .Color = RGB(255, 0, 0)

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

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

Displaying an Input Format in a Cell

Want to show a user, in a cell, what you expect their input to look like? Unfortunately, it cannot be done natively in ...

Discover More

Eliminating "Before Spacing" at the Top of a Page

When formatting paragraphs in Word, you have several options to adjust the spacing before, within, and at the end of each ...

Discover More

AutoFill with Random Numbers

When entering data into a worksheet, you may have a need to fill a range of cells with a group of random numbers. This ...

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 (ribbon)

Conditionally Setting the Color of Worksheet Tabs

If you want to set the color of a worksheet tab based on some conditions within the worksheet, you'll need to resort to ...

Discover More

Detecting Types of Sheets in VBA

When processing workbook information in a macro, you may need to step through each worksheet to make some sort of ...

Discover More

Retrieving Worksheet Names

Want to grab the names of all the worksheets in a workbook? Here's how you can stuff all those names into the cells of a ...

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 2 + 2?

2020-08-14 00:15:02

Christopher J Duston

Please clarify if the range MUST be cell A1.


2020-04-17 09:52:49

JASON MYRICK

Can this be updated to change the tab color if the tab is empty and/or if a picture is on the tab?


2019-07-17 05:36:06

Gandhi, Shreepad

Hei Willy. Tried your macro. It works well. Thanks.


2019-07-13 11:36:09

Willy Vanhaelen

Although the macro presented in this tip works, it works to well. When you make any change anywhere in this sheet the macro sets the color of the tab over and over again although cell A1 didn't change. A waste of time!!!

The following macro changes the tab color only if the value in cell A1 changes which is of course more efficient:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> "$A$1" Then Exit Sub
With Me.Tab
Select Case Target
Case 1: .Color = vbBlack
Case 2: .Color = vbRed
Case 3: .Color = vbGreen
Case 4: .Color = vbYellow
Case 5: .Color = vbBlue
Case 6: .Color = vbMagenta
Case 7: .Color = vbCyan
Case 8: .Color = vbWhite
Case Else: .ColorIndex = xlColorIndexNone
End Select
End With
End Sub


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.