Another Automatic tab color change question

usas12gthr

New Member
Joined
Jul 28, 2021
Messages
2
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have multiple, but not all, tabs that I need to have the tab color change when a certain condition is met in any of four cells. Cell C3, C4, C5, and C6 will turn red if the value is greater than the Qty purchased value of that type. Note - these "C" cells auto-calculate using CountIf on a chart that is below If any of the "C" tabs are red I need the sheet tab to go red as well. If none of the cells are red then the tab color does not change. I need this to change when the chart is updated. Thank you
1627484243995.png
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
I put the code in BOTH the Worksheet "Calculate" and Worksheet "Change" subs so you can choose which one to use or both. I'm not checking for the interior color of the cells. Rather I am checking the original condition which is if C > B for a given row in the Range C3:C6 then turn the worksheet tab red. If none of the values are > than the values in B then the worksheet tab color is removed.

VBA Code:
Private Sub Worksheet_Calculate()
    Dim r As Range
    Dim c As Range
    Dim tabRed As Boolean
    
    tabRed = False
    
    Set r = Range("C3:C6")
    
    For Each c In r
        If c.Value > c.Offset(0, -1).Value Then
            ActiveSheet.Tab.Color = 255
            tabRed = True
        End If
    Next
    
    If Not tabRed Then ActiveSheet.Tab.Color = xlNone
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim r As Range
    Dim c As Range
    Dim tabRed As Boolean
    
    tabRed = False
    
    Set r = Range("C3:C6")
    
    For Each c In r
        If c.Value > c.Offset(0, -1).Value Then
            ActiveSheet.Tab.Color = 255
            tabRed = True
        End If
    Next
    
    If Not tabRed Then ActiveSheet.Tab.Color = xlNone

End Sub
 
Upvote 0
I put the code in BOTH the Worksheet "Calculate" and Worksheet "Change" subs so you can choose which one to use or both. I'm not checking for the interior color of the cells. Rather I am checking the original condition which is if C > B for a given row in the Range C3:C6 then turn the worksheet tab red. If none of the values are > than the values in B then the worksheet tab color is removed.

VBA Code:
Private Sub Worksheet_Calculate()
    Dim r As Range
    Dim c As Range
    Dim tabRed As Boolean
   
    tabRed = False
   
    Set r = Range("C3:C6")
   
    For Each c In r
        If c.Value > c.Offset(0, -1).Value Then
            ActiveSheet.Tab.Color = 255
            tabRed = True
        End If
    Next
   
    If Not tabRed Then ActiveSheet.Tab.Color = xlNone
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim r As Range
    Dim c As Range
    Dim tabRed As Boolean
   
    tabRed = False
   
    Set r = Range("C3:C6")
   
    For Each c In r
        If c.Value > c.Offset(0, -1).Value Then
            ActiveSheet.Tab.Color = 255
            tabRed = True
        End If
    Next
   
    If Not tabRed Then ActiveSheet.Tab.Color = xlNone

End Sub
Thank you :)
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,334
Members
449,077
Latest member
Jocksteriom

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