How to change sheet tab color when dropdown cell is used

fmorenojr

New Member
Joined
Jun 22, 2023
Messages
15
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am trying to change the tab sheet color when a cell (that has a drop down) is picked. The drop down also has conditional formatting as well.

  • Sheets are named "Student1 to Student70" (they were a copied from Student1)
  • N3 is the cell that has the drop down I will be using.
  • the conditional formatting is "=$N$2" and the colors for the formatting are the same as below.
  • I am using a macros enabled sheet.

"Graduated" to change to Green.
"Released" to change to Red.
"Resigned" to change to Yellow.

I'm very green when it comes to VBA, I've tried codes I've seen online to mimic what I'm doing but to no avail. Any assistance is greatly appreciated.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Welcome to the MrExcel forum. Please accept my warmest greetings and sincere hope that all is well.

Put the following code in the events of Thisworkbook.
VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
  If Target.Count > 1 Then Exit Sub
  If Left(Sh.Name, 7) <> "Student" Then Exit Sub
 
  If Target.Address(0, 0) = "N3" Then
    Select Case Target.Value
      Case "Graduated":  Sh.Tab.Color = vbGreen
      Case "Released":   Sh.Tab.Color = vbRed
      Case "Resigned":   Sh.Tab.Color = vbYellow
    End Select
  End If
End Sub

ThisWorkbook EVENT
- Open the VB Editor (press Alt + F11).
- Over in the Project Explorer, double click on ThisWorkbook.
- In the white panel that then appears, paste the above code.

--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
 
Upvote 0
Solution
In addition t]o coloring the tabs when the dropdown is selected, this will remove the color from the tabs if a user should delete the contents of N3 or copy and paste into N3 with something other than the 3 values you list for the dropdown.
VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Not Intersect(Target, Sh.Range("N3")) Is Nothing Then
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    Select Case Target.Value
        Case "Graduated": Sh.Tab.Color = vbGreen
        Case "Released": Sh.Tab.Color = vbRed
        Case "Resigned": Sh.Tab.Color = vbYellow
        Case Else: Sh.Tab.Color = RGB(255, 255, 255)
    End Select
End If
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
End Sub
 
Upvote 0
Welcome to the MrExcel forum. Please accept my warmest greetings and sincere hope that all is well.

Put the following code in the events of Thisworkbook.
VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
  If Target.Count > 1 Then Exit Sub
  If Left(Sh.Name, 7) <> "Student" Then Exit Sub
 
  If Target.Address(0, 0) = "N3" Then
    Select Case Target.Value
      Case "Graduated":  Sh.Tab.Color = vbGreen
      Case "Released":   Sh.Tab.Color = vbRed
      Case "Resigned":   Sh.Tab.Color = vbYellow
    End Select
  End If
End Sub

ThisWorkbook EVENT
- Open the VB Editor (press Alt + F11).
- Over in the Project Explorer, double click on ThisWorkbook.
- In the white panel that then appears, paste the above code.

--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
Dante, this worked perfectly. I was having issues at first but it started working correctly after the fact.

Thank you so much!

FMJ
 
Upvote 0
Dante, this worked perfectly. I was having issues at first but it started working correctly after the fact.

Thank you so much!

FMJ
What do you want to happen to the tab color if a user deletes N3 or pastes a value not on your data validation list into N3?
 
Upvote 0
Hi @fmorenojr

If you need what Joemo recommends, you just need one line in my code, try:


VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
  If Target.Count > 1 Then Exit Sub
  If Left(Sh.Name, 7) <> "Student" Then Exit Sub
  
  If Target.Address(0, 0) = "N3" Then
    Select Case Target.Value
      Case "Graduated":  Sh.Tab.Color = vbGreen
      Case "Released":   Sh.Tab.Color = vbRed
      Case "Resigned":   Sh.Tab.Color = vbYellow
      Case Else:         Sh.Tab.ColorIndex = xlNone
    End Select
  End If
End Sub
 
Upvote 0
In addition t]o coloring the tabs when the dropdown is selected, this will remove the color from the tabs if a user should delete the contents of N3 or copy and paste into N3 with something other than the 3 values you list for the dropdown.
VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Not Intersect(Target, Sh.Range("N3")) Is Nothing Then
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    Select Case Target.Value
        Case "Graduated": Sh.Tab.Color = vbGreen
        Case "Released": Sh.Tab.Color = vbRed
        Case "Resigned": Sh.Tab.Color = vbYellow
        Case Else: Sh.Tab.Color = RGB(255, 255, 255)
    End Select
End If
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
End Sub
I do have a blank (not the word) selection in the dropdown as well. so, to remove the color should it be
Case "Blank": Sh.Tab.Color = RGB(255, 255, 255)
 
Upvote 0
I do have a blank (not the word) selection in the dropdown as well. so, to remove the color should it be
See post #3 or post #6 for removing the tab color when N3 is blank or contains a value not on your dropdown list.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
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