Dynamically Changing Worksheet Tab Color

by Allen Wyatt
(last updated June 14, 2014)

14

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)

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13138) applies to Microsoft Excel 2007, 2010, and 2013.

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

Turning Off Figure Caption Numbering

Ever want to use Word's automatic figure captioning feature, without the numbering? While there isn't a way to make this work ...

Discover More

Rounding to Two Significant Digits

Excel provides a variety of functions you can use to round values in any number of ways. It does not, however, provide a way ...

Discover More

Arranging Paragraphs

Need to move a few paragraphs around in your document? Word provides a couple of handy shortcuts that make it very easy to ...

Discover More

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!

More ExcelTips (ribbon)

Copying a Worksheet

Need to make a copy of one of your worksheets? Excel provides a few different ways you can accomplish the task.

Discover More

Dynamic Worksheet Tab Names

Need a worksheet's name to change based on the contents of a cell? You'll need to rely on a macro to do the changing, but it ...

Discover More

Naming Tabs for Weeks

Need to set up a workbook that includes a worksheet for each week of the year? Here's a couple of quick macros that can do ...

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}] in your comment text. You’ll be prompted to upload your image when you submit the comment. 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 9 + 4?

2017-02-01 09:44:14

Sally Mizell

How would I change the tab color based on a cell in a different worksheet within the same Excel book? I tried just adding the tab name to the range ("'Book'!A3") But it is giving me a Run Time Error '1004'


2016-05-18 10:25:04

Dave

I have been using the above code for about a month. It worked great. Yesterday I opened my workbook and there was an error. No matter what color I enter I get "Run-Time Error 50290: application-defined or object-defined error." I had been unlocking and locking worksheets earlier in the day. I have tried copying the code to a new sheet and still get the same error if the new worksheet is created with the original worksheet open. The code works if I completely close Excel and re-open.
Does anyone have any ideas?
Thanks


2016-01-22 15:55:38

Matt

Or even better would be to search for any cell within Columns O through Q that contain the word "Call", so I could rid my worksheet of an extra formula.

Also, another error I just ran into is that the macro is affecting other tabs:
-I have Sheet1 set automatically copy the active row to Sheet7 (where i have this macro). When the row is copied over, it activates this macro, but instead of effecting Sheet7's tab color, it changes Sheet1's tab color because the "ActiveSheet" in that instance is Sheet1, not Sheet7.

Again, any help would be appreciated. Thanks!


2016-01-22 15:36:47

Matt

This works well if I only want the macro to look at 1 cell. Is there a way for it to look at 3 different cells and if any of those cells contain certain text it will change the tab color?

I have cells O1:Q1 show "Calls To Be Made" with a formula to look for certain text in each corresponding column. If "Calls To Be Made" appears in any of those three cells, I want the tab color to change to red.

Any Help would be appreciated. Thanks.


2016-01-10 09:19:10

janardon

my tab color updates only after i activate a certain worksheet.

how to change the color simultaneously of all the tabs of my workbook.

eg. my workbook contains 30 worksheets for each day of a month and I wish to put red color in all the Sundays.

Presently I require to input the first date and formula takes care of the remaining days.
Using fn weekday, sundays correspond to 7 which changes the tab color to red.

So if I change the date of the first sheet, all the subsequent date changes but only after i click the tabs, the color changes.


2015-12-13 17:51:00

Stacie

Hi Allen,

I've used this code, but I'm wondering how I would use it for more than one tab. So my main sheet is called "Summary" and my other sheets are called "Estimate1" and "Estimate2". I've put the following code in the "Summary" sheet:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
MyVal = Range("H5").Text

With Sheet2.Tab
Select Case MyVal
Case ""
.ColorIndex = 2
Case "Not Signed"
.ColorIndex = 45
Case "Signed"
.ColorIndex = 43
Case "Closed"
.ColorIndex = 3
End Select
End With
End Sub

This changes the tab color for "Estimate1" depending on cell H5. How can I add to this to change the tab color of "Estimate2" depending on cell H6?


2015-11-25 09:04:50

David

Very interesting tip! I was wondering if it is possible to modify the code so that the worksheet tab is filled with a colour if any cell in the worksheet is filled with a colour.

I have a workbook with multiple sheets. In each sheet I have conditional formatting on some cells (not the same cells in each sheet) that will fill with a colour if the value is not nil (they work like a visual warning to me). I don't know if there is a way to set worksheet tabs to automatically be filled if that worksheet contains a 'warning'.

I hope I explained myself well, and any help is appreciated.

Thanks,
David


2015-01-26 00:04:51

john

I relised the initial macro is able to achieve this! thank you


2015-01-26 00:01:07

john

If i have a workbook with 19 spreadsheets, is there any macro I can include to only change the colors of selected spreadsheets

For instance, if cell A1 has a value of 1, i only want the tabs of spreadsheet 1,3 and 5 to change colors

or if cell A1 has a value of 2, i only want the tabs of spreadsheet 7,9, and 11 to change colors

and so on

Thank you

John


2014-06-21 12:24:02

Willy Vanhaelen

@Garry: make sure you copy the macro in the sheet's code page. Not to a module. Right click the sheet tab and select 'View Code': that's where you have to copy the macro.


2014-06-19 16:29:23

Glenn Case

Gary:

Make sure that you don't have a macro somewhere setting Application.EnableEvents = False. (I have seen this happen when a macro terminates with an error, and there is no error trapping to reset it.)

As a quick check to see if this is the issue, you can enter Application.EnableEvents = True into your immediate window in the VBA editor to ensure events are enabled, then try the code.


2014-06-19 10:57:08

Gary Lundblad

Thank you Willy; however; no code at the worksheet level seems to do anything for me. There must be a setting somewhere. I've tried other macros at the worksheet level, and none have ever worked. Any ideas?

Thank you!

Gary


2014-06-18 08:25:17

Willy Vanhaelen

@Gary: Make shure you format A1 as General or Text.

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 <> Range("A1") Then Exit Sub
With Me.Tab
Select Case Range("A1")
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

With this macro the formatting of cell A1 doesn't matter.


2014-06-17 11:10:44

Gary Lundblad

This is the second tip now that uses code pasted into an individual worksheet that does not do anything. Does anyone know of a setting that might be hindering these types of macros? Other macros seem to function fine.

Thank you!

Gary


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.