Wouldn’t it be great to change the color of a worksheet tab based on a calculation? If you’re not convinced, here are some possible scenarios:
- When a schedule reconciles or includes a specific cell value
- If there is an error in a range of cells
- To color code tabs to match a contents page
You were unsure at first, weren’t you? But now you think it would be a good idea, don’t you? The good news is, it’s pretty simple.
It just requires a small amount of code in a User Defined Function.
I didn’t think up this idea by myself. The idea came from David Hager’s post, check out his post here.
Creating the User Defined Function
To create a User Defined Function click Developer -> Visual Basic (or shortcut ALT + F11).
The Visual Basic Editor window will open, click Insert -> Module
Add the code as shown below. The code for User Defined Functions must be within a standard module to work correctly.
Function TabColor(CellColor As Range, Optional SheetName As String, _ Optional WorkbookName As String) Application.Volatile If SheetName = "" Then SheetName = Application.Caller.Parent.Name End If If WorkbookName = "" Then WorkbookName = Application.Caller.Parent.Parent.Name End If If CellColor.Interior.Color = xlNone Then 'If cell color is no fill then make tab color automatic Workbooks(WorkbookName).Sheets(SheetName).Tab.Color = xlAutomatic Else 'Else change to the cell color Workbooks(WorkbookName).Sheets(SheetName).Tab.Color = CellColor.Interior.Color End If TabColor = "" End Function
Depending on how you wish the User Defined Function to operate, there are a variety of ways to use this function. See the examples in the section below.
Using the Function
The Function we created above is now available within Excel for use, just like any other function. The syntax is as follows:
- CellColor is a reference to a cell containing the color to be used
- SheetName is an optional argument to apply the function to another sheet in the same workbook
- WorkbookName is an optional argument to apply the function to another sheet in a different workbook
Now, let’s look at some examples.
Example #1 – Basic usage
This examples shows the basic usage of the TabColor User Defined Function.
Cell A1 contains the following formula:
The tab color in Cell B1 is applied to the sheet within which the function is used.
Cell A2 contains the following formula:
The tab color in Cell B2 is applied to Sheet2 in the same workbook.
Cell A3 contains the following formula:
The tab color in Cell B3 is applied to Sheet1 of the TestBook.xlsx workbook (which needs to be open for the function to work).
Example #2: Where a cell includes a specific cell value
This example shows how to set the tab color based on a cell value.
The formula in cell C4 is:
Where the Total equals 100 the tab color is green, otherwise it is red.
Example #3: Error in a range
The example shows the tab color changing where an error exists within a range.
The formula in Cell D2 is:
The SUMPRODUCT element of the formula, counts the number of errors. As there is one error in Cell B7 the IF function will show the TRUE result, which is causing the TabColor function to turn the tab color red.
Example #4: Color code tabs to a contents page
This example shows the Sheet Name being constructed from two cells.
The formula in Cell D4 is:
NY in Cell B4 and Sales in Cell D3 are concatenated with the ” & ” symbol. The TabColor function is causing the “NY Sales” tab to turn light green.
As the TabColor function is not using calculated values it is able to point to it’s own cell without causing a circular reference.
Use RGB to apply the color
In the UDF above, the color is set by linking to a cell, by changing the code it is possible to use the RGB values instead.
Function TabColorRGB(Red As Integer, Green As Integer, Blue As Integer, _ Optional SheetName As String, Optional WorkbookName As String) Application.Volatile If SheetName = "" Then SheetName = Application.Caller.Parent.Name End If If WorkbookName = "" Then WorkbookName = Application.Caller.Parent.Parent.Name End If Workbooks(WorkbookName).Sheets(SheetName).Tab.Color = RGB(Red,Green,Blue) TabColor = "" End Function
This function can be used as follows:
The first 3 arguments are the Red, Green and Blue color codes. The Sheet name and Workbook name are both optional. The formula above will turn the tab for Sheet1 of TestBook.xlsx to a red (which is the color of Red = 255, Green = 0 and Blue = 0)
Changing the tab color using a formula is quite easy and can provide a simple visual representation of the status of a worksheet within the workbook.
About the author
Hey, I’m Mark, and I run Excel Off The Grid.
My parents tell me that at the age of 7 I declared I was going to become a qualified accountant. I was either psychic or had no imagination, as that is exactly what happened. However, it wasn't until I was 35 that my journey really began.
In 2015, I started a new job, for which I was regularly working after 10pm. As a result, I rarely saw my children during the week. So, I started searching for the secrets to automating Excel. I discovered that by building a small number of simple tools, I could combine them together in different ways to automate nearly all my regular tasks. This meant I could work less hours (and I got pay raises!). Today, I teach these techniques to other professionals in our training program so they too can spend less time at work (and more time with their children and doing the things they love).
Do you need help adapting this post to your needs?
I'm guessing the examples in this post don't exactly match your situation. We all use Excel differently, so it's impossible to write a post that will meet everybody's needs. By taking the time to understand the techniques and principles in this post (and elsewhere on this site), you should be able to adapt it to your needs.
But, if you're still struggling you should:
- Read other blogs, or watch YouTube videos on the same topic. You will benefit much more by discovering your own solutions.
- Ask the 'Excel Ninja' in your office. It's amazing what things other people know.
- Ask a question in a forum like Mr Excel, or the Microsoft Answers Community. Remember, the people on these forums are generally giving their time for free. So take care to craft your question, make sure it's clear and concise. List all the things you've tried, and provide screenshots, code segments and example workbooks.
- Use Excel Rescue, who are my consultancy partner. They help by providing solutions to smaller Excel problems.
Don't go yet, there is plenty more to learn on Excel Off The Grid. Check out the latest posts: