简体   繁体   中英

Change sheet tab color based on cell value?

I am trying to create a macros that changes the tab color based on the name appearing in a specific cell on a sheet. The names appear in cell b11 . I have seven different names that may pop up depending which means that the specific sheet is for them to use. I am trying to tie a color to their name and have the color reflect on the tab.

This is the formula that I tried and used but honestly, I'm just shooting in the dark here. I have no idea what I am doing.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$11" Then
  
    Case "Arica"
        Me.Tab.Color = vbYellow
    Case "Amy"
        Me.Tab.Color = vbGreen
    Case "Nadia"
        Me.Tab.Color = vbBlue
    Case "Roelisa"
        Me.Tab.Color = vbOrange
    Case "Wezi"
        Me.Tab.Color = vbPurple
    Case "Mabel"
        Me.Tab.Color = vbPink
    Case "Patrice"
        Me.Tab.Color = vbLightBlue
    End Select
End If
End Sub

Is there anyone who can help me write a formula that executes what I desire it to do please? Thanks!

  • You need to open the Select statement properly. ( Select Case Target )
  • You need to end the procedure properly. ( End Sub )

A quick look in the documentation often helps with syntax. Just saying.

https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/select-case-statement

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$11" Then
    Select Case Target
        Case "Arica"
            Me.Tab.Color = vbYellow
        Case "Amy"
            Me.Tab.Color = vbGreen
        Case "Nadia"
            Me.Tab.Color = vbBlue
        Case "Roelisa"
            Me.Tab.Color = vbOrange
        Case "Wezi"
            Me.Tab.Color = vbPurple
        Case "Mabel"
            Me.Tab.Color = vbPink
        Case "Patrice"
            Me.Tab.Color = vbLightBlue
    End Select
End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM