Tab ColorIndex重置excel vba

时间:2017-01-07 12:52:00

标签: excel vba excel-vba colors tabs

我编写了一个VBA脚本,它根据单元格值更改了工作表选项卡的颜色。 当我输入值时脚本运行良好,但是当我清除单元格的内容值时,脚本出现故障并且颜色选项卡未重置。

本工作手册上的脚本:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Not Application.Intersect(Target, Range("BH37")) Is Nothing Then
    Call CouleurOnglet(Sh, Target)
End If
End Sub

模块上的脚本:

Sub CouleurOnglet(Sh As Object, Target As Range)
With Sh
    Select Case Target
    Case Is = "OK"
        .Tab.ColorIndex = 4 'Couleur verte
    Case Is = "NOK"
        .Tab.ColorIndex = 3 'Couleur rouge
    Case Is = "A vérifier"
        .Tab.ColorIndex = 45 'Couleur orange
    Case Else
        .Tab.ColorIndex = xlAutomatic 'reset couleur
    End Select
End With
End Sub

enter image description here

1 个答案:

答案 0 :(得分:3)

选项卡的默认颜色为ColorIndex值为-4142或xlcolorindexnone的颜色。

如果你改为:

.Tab.Color = xlAutomatic

代码:

.Tab.ColorIndex = xlColorIndexNone

它应该按照需要工作。