Change a tab color based on a cell value with a formula automatically

crystaljulius

New Member
Joined
Jul 22, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi, i am an accountant and need the Tab color to change based on a cell value with a formula automatically. I currently have the below code. It works but only when I click on to the Tab. Can this be changed so that when I update my master sheet it automatically changes the tab color without clicking on the tab.

Private Sub Worksheet_Change(ByVal Target As Range)
If Range("F18").Value <> 0 Then
Me.Tab.ColorIndex = 4
Else
Me.Tab.ColorIndex = xlColorIndexNone
End If
End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi & welcome to MrExcel.
You can use the calculate event instead, although this will run when any formula on the sheet recalculates, so it could slow your workbook down.
VBA Code:
Private Sub Worksheet_Calculate()
If Range("F18").Value <> 0 Then
Me.Tab.ColorIndex = 4
Else
Me.Tab.ColorIndex = xlColorIndexNone
End If
End Sub
 
Upvote 0
Hi and Welcome...

My suggestion would maybe be the below...(Not sure how many sheets you need this to work on... This gets placed in the ThisWorkbook section and will update the mentioned sheets Tab color whenever a different sheet... maybe Master Sheets is updated I linked my test with a formula)

VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If Worksheets("Sheet1").Range("F18").Value <> 0 Then
        Worksheets("Sheet1").Tab.ColorIndex = 4
    Else
        Worksheets("Sheet1").Tab.ColorIndex = xlColorIndexNone
    End If
End Sub
 
Upvote 0
That will only work if a cell is changed manually, not via a formula.
 
Upvote 0
What is the formula in F18?
What tab is that formula on?
 
Upvote 0
Hi Fluff... Apologies...maybe I should have stated that what I did with my test is I put on Sheet1 in F18 the following:

Rich (BB code):
=Sheet2!A1

If on Sheet 2 I change the number then the tab color does change...
 
Upvote 0
I think to avoid calculations etc... I would create a lookup table containing each sheet name and next to it a link to the cell (i.e. F18), you could even have the colour you want the tab to go next to that.

Then i would create a macro that would fire whenever you leave the 'Master' sheet that would then colour the tabs based on the lookup table that was created.

The benefit of doing it this way will mean that the tabs colours would only be changed once when you leave the Master tab, the drawbacks are having to keep the lookup table updated every time you add a new sheet.

The only event that will ever be fired with this method is the one when leaving the Master sheet. (Worksheet_Deactivate)

Lookup table would look something like the below:
Book1
ABC
1Sheet NameLink to cellColour
2Sheet20vbGreen
3Sheet37vbRed
4Sheet40vbBlue
Sheet1
Cell Formulas
RangeFormula
B2B2=Sheet2!F18
B3B3=Sheet3!F18
B4B4=Sheet4!F18
 
Upvote 0
Changed it slightly, so I decided to keep to ColorIndex as that was what was being used.

Lookup tab named: Lookup
Table on the Lookup tab:
Book1
ABC
1Sheet NameLink to cellColour Index
2Sheet204
3Sheet374
4Sheet404
Lookup
Cell Formulas
RangeFormula
B2B2=Sheet2!F18
B3B3=Sheet3!F18
B4B4=Sheet4!F18


Code that I put into the Master tab:
VBA Code:
Private Sub Worksheet_Deactivate()
    Dim wsLookup As Worksheet
    Dim tLookup As Range
    Dim rCell As Range
    
    Set wsLookup = Sheets("Lookup")
    Set tLookup = wsLookup.ListObjects("Table1").ListColumns(1).DataBodyRange
    
    For Each rCell In tLookup.Cells
        If rCell.Offset(, 1) <> 0 Then
            Sheets(rCell.Value).Tab.ColorIndex = rCell.Offset(, 2).Value
        End If
    Next rCell
End Sub
 
Upvote 0
Hi & welcome to MrExcel.
You can use the calculate event instead, although this will run when any formula on the sheet recalculates, so it could slow your workbook down.
VBA Code:
Private Sub Worksheet_Calculate()
If Range("F18").Value <> 0 Then
Me.Tab.ColorIndex = 4
Else
Me.Tab.ColorIndex = xlColorIndexNone
End If
End Sub
Thank you. This works. If I have any problems will try the other solutions.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,326
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top