Changing color of tabs in Excel using VBA?

前端 未结 2 1168
星月不相逢
星月不相逢 2020-12-11 21:09

I have script I created that creates a new sheet, and a new tab in said sheet, before changing the names of said tabs, I want to make it so Sheet1 through Sheet4 all are red

相关标签:
2条回答
  • 2020-12-11 21:26

    At first I thought using the Sheets(Array("Sheet1","Sheet2")).Select method would work, but after testing (and running the macro recorder), I found it didn't.

    Update

    ooo had a great comment. This is cleaner, easier to read way to go:

    Sub SheetTabColor()
    
    Dim mySheets As Worksheets
    Dim mySheet As Worksheet
    
    Set mySheets = Sheets(Array("Sheet1", "Sheet2", "Sheet3", "Sheet4"))
    
    For Each mySheet In mySheets
        mySheet.Tab.Color = 255
    Next
    
    
    End Sub
    

    This was my original. Same idea, slightly different method:

    Sub SheetTabColor()
    
    Dim arrSheets() As String
    
    arrSheets() = Split("Sheet1,Sheet2,Sheet3,Sheet4", ",")
    
    Dim i As Integer
    For i = LBound(arrSheets()) To UBound(arrSheets())
    
        Sheets(arrSheets(i)).Tab.Color = 255
    
    Next
    
    End Sub
    
    0 讨论(0)
  • 2020-12-11 21:37

    Try something like this:

    Dim Sht As Worksheet
    For Each Sht In Application.Worksheets
        With Sht.Tab
            .Color = 255
        End With
    Next Sht
    

    Note:

    I think you may be able to change Application.Worksheets to wbBK2.Worksheets, but I'm running too low on caffeine to remember, or time to test it.

    0 讨论(0)
提交回复
热议问题