根据单元格值更改标签颜色

时间:2019-06-27 13:14:45

标签: excel vba

我有一个带有多个选项卡的Excel工作簿,其中大多数包含项目信息以及用于报告项目状态的特定单元格。我尝试将一个宏组合在一起以根据项目状态设置选项卡颜色(以便于识别)。工作簿中有一些选项卡不包含此项目状态单元格,我试图从例程中排除它们。

Sub Set_tab_color()
    Dim WS As Worksheet
    For Each WS In ActiveWorkbook.Worksheets

    If Not WS.Name = "Cover" _
    And Not WS.Name = "Due Dill" _
    And Not WS.Name = "Comm '19" _
    And Not WS.Name = "Comm '18" _
    And Not WS.Name = "Comm '17" _
    And Not WS.Name = "Clarizen_PLI" _
    And Not WS.Name = "Clarizen_milestones" _
    And Not WS.Name = "_blank" Then
    End If

        Select Case WS.Range("B5").Value
        Case "C"
            WS.Tab.Color = RGB(0, 176, 240)
        Case "R/C"
            WS.Tab.Color = RGB(192, 0, 0)
        Case "R"
            WS.Tab.Color = RGB(255, 0, 0)
        Case "A"
            WS.Tab.Color = RGB(255, 192, 0)
        Case "G"
            WS.Tab.Color = RGB(0, 176, 80)
        Case Else
            WS.Tab.ColorIndex = xlColorIndexNone
        End Select
    Next WS
End Sub

调试亮点案例“ C”出现错误:

  

运行时错误'13'。类型不匹配。

有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:1)

您可以尝试:

Option Explicit

Sub Set_tab_color()

    Dim WS As Worksheet

    For Each WS In ActiveWorkbook.Worksheets

        With WS

            If Not .Name = "Cover" _
                And Not .Name = "Due Dill" _
                And Not .Name = "Comm '19" _
                And Not .Name = "Comm '18" _
                And Not .Name = "Comm '17" _
                And Not .Name = "Clarizen_PLI" _
                And Not .Name = "Clarizen_milestones" _
                And Not .Name = "_blank" Then

                Select Case .Range("B5").Value

                Case "C"
                    .Tab.Color = RGB(0, 176, 240)
                Case "R/C"
                    .Tab.Color = RGB(192, 0, 0)
                Case "R"
                    .Tab.Color = RGB(255, 0, 0)
                Case "A"
                    .Tab.Color = RGB(255, 192, 0)
                Case "G"
                    .Tab.Color = RGB(0, 176, 80)
                Case Else
                    .Tab.ColorIndex = xlColorIndexNone
                End Select

            End If

        End With

    Next WS

End Sub