Dynamically Changing Worksheet Tab Color

by Allen Wyatt
(last updated May 10, 2019)


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)


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 Office 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. ...


Hiding Spelling Errors

When you are typing in a document, Word normally checks your spelling in the background, marking possible spelling errors ...

Discover More

Formatting Raw Data

When you get a bunch of raw data into Excel from an external source, it isn't going to be formatted to your liking. The ...

Discover More

Hiding Errors on Printouts

If there are error values in a worksheet, you may not want those error values to appear on a printout. Excel actually ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Switching Headers in a Frozen Row

Excel allows you to "freeze" rows in your worksheet. What if you want the rows that are frozen to change as you scroll ...

Discover More

Jumping to Alphabetic Worksheets

Got a workbook with a lot of worksheets in it? Here are some handy ways to jump to the worksheet you want, alphabetically.

Discover More

Freezing Worksheet Tabs

If you have a lot of worksheets in a workbook, you may wonder if you can "freeze" the position of some of those worksheet ...

Discover More

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.


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. 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 1 + 3?

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


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?

2016-01-22 15:55:38


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


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


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


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


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.


2015-01-26 00:04:51


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

2015-01-26 00:01:07


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


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


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!


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!


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

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.