Change Tab Color Based on Cell Value

ScottA333

New Member
Joined
Sep 14, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I have been trying to use a few Macros that I've found online to change each tab color on my workbook referencing cell E1 on each tab with no luck. I would like the tab to be red if E1 does not equal 0, green if it does equal 0.

Any help would be greatly appreciated!
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
If you want the tab colour to change dynamically when cell E1 on the sheet is changed , put this code in the ThisWorkbook module:
VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If Sh.Range("E1").Value = 0 Then
        Sh.Tab.Color = vbGreen
    Else
        Sh.Tab.Color = vbRed
    End If
End Sub
On the other hand, if you only want to change all the tabs when you run a macro, put this code in a standard module:
VBA Code:
Public Sub Change_Tabs_Colour()
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Range("E1").Value = 0 Then
            ws.Tab.Color = vbGreen
        Else
            ws.Tab.Color = vbRed
        End If
    Next
End Sub
 
Upvote 1
@CesarF Can you please explain how conditional formatting is meant to change the colour of the tab? That link is to change the colour of cells.
 
Upvote 0
If you want the tab colour to change dynamically when cell E1 on the sheet is changed , put this code in the ThisWorkbook module:
VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If Sh.Range("E1").Value = 0 Then
        Sh.Tab.Color = vbGreen
    Else
        Sh.Tab.Color = vbRed
    End If
End Sub
On the other hand, if you only want to change all the tabs when you run a macro, put this code in a standard module:
VBA Code:
Public Sub Change_Tabs_Colour()
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Range("E1").Value = 0 Then
            ws.Tab.Color = vbGreen
        Else
            ws.Tab.Color = vbRed
        End If
    Next
End Sub
@John_w

When I enter the code into the ThisWorkbook module then hit play it requests that I name the macro so I named it SheetChange but nothing is happening

First, should it be asking for a name? I have used other macros that have worked and they do not ask for a name. If it shouldn't be asking for a name, are you able to tell what I'm doing wrong?

Thank you!

1694798844367.png
 
Upvote 0
You don't run or 'play' the macro; it runs automatically when cell E1 on any sheet is changed.

You've put the code in the correct place.

Put this in the ThisWorkbook module too, so that the tab's colour is changed when you click the tab and you should see the effect of the code.

VBA Code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    If Sh.Range("E1").Value = 0 Then
        Sh.Tab.Color = vbGreen
    Else
        Sh.Tab.Color = vbRed
    End If
End Sub
 
Upvote 1
Solution
You don't run or 'play' the macro; it runs automatically when cell E1 on any sheet is changed.

You've put the code in the correct place.

Put this in the ThisWorkbook module too, so that the tab's colour is changed when you click the tab and you should see the effect of the code.

VBA Code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    If Sh.Range("E1").Value = 0 Then
        Sh.Tab.Color = vbGreen
    Else
        Sh.Tab.Color = vbRed
    End If
End Sub
Thank you @John_w !
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,876
Members
452,363
Latest member
merico17

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