VBA to change Tab Color

dchaney

Well-known Member
Joined
Jun 4, 2008
Messages
732
Office Version
  1. 2016
Platform
  1. Windows
First i would like to thank JoeMo for the following code, but I have questions about it...

PHP:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim myRng As Range, vRng As Range
Set myRng = Range("C14")
If Intersect(myRng, Target) Is Nothing Then Exit Sub
For i = 1 To myRng.Areas.Count
    For Each c In myRng.Areas(i).Cells
        If Not IsEmpty(c) And (c.Value > 5) Then
            Me.Tab.ColorIndex = 3
            Exit Sub
        End If
    Next c
    Me.Tab.ColorIndex = 4
Next i
End Sub

I have modified the original code a bit, but what I am trying to do is make the tab colors change automaticly. Currently I have to double click within cell C1 in order for the tab to change if there is a change in the number. I would like to know if there is something I can add to this code that will make the tab change color automaticly (My C! cell is a formula to give an average of days)
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Maybe

Code:
Private Sub Worksheet_Calculate()
If Range("C14").Value > 5 Then
    Me.Tab.ColorIndex = 3
Else
    Me.Tab.ColorIndex = 4
End If
End Sub
 
Upvote 0
First i would like to thank JoeMo for the following code, but I have questions about it...

PHP:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim myRng As Range, vRng As Range
Set myRng = Range("C14")
If Intersect(myRng, Target) Is Nothing Then Exit Sub
For i = 1 To myRng.Areas.Count
    For Each c In myRng.Areas(i).Cells
        If Not IsEmpty(c) And (c.Value > 5) Then
            Me.Tab.ColorIndex = 3
            Exit Sub
        End If
    Next c
    Me.Tab.ColorIndex = 4
Next i
End Sub

I have modified the original code a bit, but what I am trying to do is make the tab colors change automaticly. Currently I have to double click within cell C1 in order for the tab to change if there is a change in the number. I would like to know if there is something I can add to this code that will make the tab change color automaticly (My C! cell is a formula to give an average of days)
If your formula points to other cells on the same sheet the formula is in, then you can use this code...

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim TargetRange As Range, vRng As Range
  Set TargetRange = Intersect(Target.Dependents, Range("C14"))
  If Not TargetRange Is Nothing Then
    If TargetRange.Value > 5 Then
      Me.Tab.ColorIndex = 3
    Else
      Me.Tab.ColorIndex = xlColorIndexNone
    End If
    Exit Sub
  End If
End Sub
 
Upvote 0
If your formula points to other cells on the same sheet the formula is in, then you can use this code...

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim TargetRange As Range, vRng As Range
  Set TargetRange = Intersect(Target.Dependents, Range("C14"))
  If Not TargetRange Is Nothing Then
    If TargetRange.Value > 5 Then
      Me.Tab.ColorIndex = 3
    Else
      Me.Tab.ColorIndex = xlColorIndexNone
    End If
    Exit Sub
  End If
End Sub
Rick this worked great, thank you very much
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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