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). Follow these instructions if the Developer Tab is not visible.
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 Workbooks(WorkbookName).Sheets(SheetName).Tab.Color = CellColor.Interior.Color 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.
Generate accurate VBA code in seconds with AutoMacro
AutoMacro is a powerful VBA code generator that comes loaded with an extensive code library and many other time-saving tools and utilities.
Whether you’re an experienced coder looking to save time, or a newbie just trying to get things to work, AutoMacro is the tool for you.
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.
If you’ve found this post useful, or if you have a better approach, then please leave a comment below.
Do you need help adapting this to your needs?
I’m guessing the examples in this post didn’t exactly meet 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: