• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

How to add a 2nd color?

Eloise T

Active Member
Below is my VBA that allows me to toggle between magenta and green and none (default) tab colors. I would like to add a third color. How do I change to VBA code to allow a third color? ...e.g. black. So it would go from none (default) to magenta to green to black.


Sub Swap_Tab_Color()
Application.ScreenUpdating = False
a_tab = ActiveSheet.Name
a_color = Worksheets(a_tab).Tab.ColorIndex

' 1 = black, 3 = red, 4 = green, 7 = magenta

If a_color = 7 Then
a_color = 4
ElseIf a_color = 4 Then
a_color = xlNone
Else: a_color = xlNone
a_color = 7

End If
Worksheets(a_tab).Tab.ColorIndex = a_color
End Sub
 
Hi ,

Try this :
Code:
Sub Swap_Tab_Color()
    Dim colorindexarray As Variant
   
    colorindexarray = Array(-4142, 7, 4, 3, 1)
   
    a_tab = ActiveSheet.Name
    a_color = Worksheets(a_tab).Tab.ColorIndex

'  1 = black, 3 = red, 4 = green, 7 = magenta

    For i = 0 To UBound(colorindexarray)
        If a_color = colorindexarray(i) Then
          a_color = colorindexarray((i + 1) Mod (UBound(colorindexarray) + 1))
          Exit For
        End If
    Next
   
    Worksheets(a_tab).Tab.ColorIndex = a_color
End Sub
Narayan
 
Back
Top