VBA Code help

jlabrecque

New Member
Joined
Nov 7, 2018
Messages
14
I am trying to write a code that changes the color of tabs based on a count if function. The problem I am running into is that for the changes to take place, I have to click on one of the cells and press enter, and then all the tabs change to the appropriate color. I want the tab colors to change progressively as the total gets higher or lower.

Please see the code below

Private Sub Worksheet_Change(ByVal Target As Range)

Dim Ws1 As Worksheet
Dim Ws2 As Worksheet
Dim Ws3 As Worksheet
Dim Ws4 As Worksheet

Set Ws1 = Worksheets("1")
Set Ws2 = Worksheets("2")
Set Ws3 = Worksheets("3")
Set Ws4 = Worksheets("4")

If Range("Risk1").Value >= 5 Then
Ws1.Tab.ColorIndex = 3
ElseIf Range("Risk1") >= 3 Then
Ws1.Tab.ColorIndex = 6
ElseIf Range("Risk1") <> 0 Then
Ws1.Tab.ColorIndex = 10
Else
Ws1.Tab.ColorIndex = xlColorIndexNone
End If

If Range("Risk2").Value >= 5 Then
Ws2.Tab.ColorIndex = 3
ElseIf Range("Risk2") >= 3 Then
Ws2.Tab.ColorIndex = 6
ElseIf Range("Risk2") <> 0 Then
Ws2.Tab.ColorIndex = 10
Else
Ws2.Tab.ColorIndex = xlColorIndexNone
End If

If Range("Risk3").Value >= 5 Then
Ws3.Tab.ColorIndex = 3
ElseIf Range("Risk3") >= 3 Then
Ws3.Tab.ColorIndex = 6
ElseIf Range("Risk3") <> 0 Then
Ws3.Tab.ColorIndex = 10
Else
Ws3.Tab.ColorIndex = xlColorIndexNone
End If


If Range("Risk4").Value >= 5 Then
Ws4.Tab.ColorIndex = 3
ElseIf Range("Risk4") >= 3 Then
Ws4.Tab.ColorIndex = 6
ElseIf Range("Risk4") <> 0 Then
Ws4.Tab.ColorIndex = 10
Else
Ws4.Tab.ColorIndex = xlColorIndexNone
End If

End Sub


Thank you in advance
 
Last edited:

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Do the named ranges "RiskN" where N= 1,2,3,4 hold formulas or constants that are changed by the user? If the former, consider using a Worksheet_Calculate event rather than Worksheet_Change. If the latter, then you need the Worksheet_Change for the tab colors to change progressively as the total gets higher or lower.
 
Upvote 0

Forum statistics

Threads
1,223,954
Messages
6,175,603
Members
452,658
Latest member
GStorm

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