Consulting

Results 1 to 4 of 4

Thread: Solved: Changing tab color

  1. #1
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location

    Solved: Changing tab color

    I am trying to adapt this to chage the tab colors. The problem is that no matter what value I use for the tab color, it always sets it to black. Are there seperate color indexes for the tab colors?

    [VBA] Sub WorksheetLoop()
    Dim WS_Count As Integer
    Dim I As Integer, ws As Variant
    WS_Count = ActiveWorkbook.Worksheets.Count

    For I = 1 To WS_Count
    If ActiveWorkbook.Worksheets(I).Name = "Sheet1" Then
    ActiveWorkbook.Worksheets(I).Tab.Color = 15
    Else
    If ActiveWorkbook.Worksheets(I).Name = "Sheet2" Then
    ActiveWorkbook.Worksheets(I).Tab.Color = 25
    End If
    End If
    Next I
    End Sub [/VBA]

    Also, can this be accomplished with a Select Case statement? Thanks
    Peace of mind is found in some of the strangest places.

  2. #2
    Knowledge Base Approver
    The King of Overkill!
    VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Hi Austenr,

    You wouldn't need a select case statement here, since you only have 2 sheets you're doing this to. I can't test this, since I can't change my tab colors (xl2k), but you could use something like:[vba]Sub WorksheetLoop()
    Dim I As Integer
    For I = 1 To ActiveWorkbook.Worksheets.Count
    If ActiveWorkbook.Worksheets(I).Name = "Sheet1" Then
    ActiveWorkbook.Worksheets(I).Tab.Color = 15
    ElseIf ActiveWorkbook.Worksheets(I).Name = "Sheet2" Then
    ActiveWorkbook.Worksheets(I).Tab.Color = 25
    End If
    Next I
    End Sub[/vba]
    But you could also just use:[vba]Sub WorksheetLoop()
    On Error Resume Next
    Sheets("Sheet1").Tab.Color = 15
    Sheets("Sheet2").Tab.Color = 25
    On Error GoTo 0
    End Sub[/vba]Seems like that would be the easier way to go.
    Matt

  3. #3
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Unable to test (also running XL2K at work)..but I think all you need to do is to use ColorIndex instead of color.

    ...This is just a guess from what I've found online:



    [VBA]
    Sub WorksheetLoop()

    Dim WS_Count As Integer
    Dim I As Integer, ws As Variant
    WS_Count = ActiveWorkbook.Worksheets.Count

    For I = 1 To WS_Count
    If ActiveWorkbook.Worksheets(I).Name = "Sheet1" Then
    ActiveWorkbook.Worksheets(I).Tab.ColorIndex = 15
    Else
    If ActiveWorkbook.Worksheets(I).Name = "Sheet2" Then
    ActiveWorkbook.Worksheets(I).Tab.ColorIndex = 25
    End If
    End If
    Next I
    End Sub

    [/VBA]

    EDIT: Here is the site I was talking about:
    http://msdn.microsoft.com/library/de...HV03076651.asp





    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  4. #4
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    ok thanks. Can you offer suggestions on another post I just did concerning the offset ptoperty? Thanks
    Peace of mind is found in some of the strangest places.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •