使用 VBA 更改 Excel 中选项卡的颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12464457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 14:01:41  来源:igfitidea点击:

Changing color of tabs in Excel using VBA?

excelexcel-vbavba

提问by Matt Ridge

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.

我创建了一个脚本,用于创建一个新工作表和该工作表中的一个新选项卡,在更改所述选项卡的名称之前,我想让它使 Sheet1 到 Sheet4 都是红色的。

With wbBK2.Sheets(wsWS1).Tab
    .Color = 255
End With

Now the code above works for individual tabs, but I am wondering is there a way to change all four tabs at the same time using Excel VBA?

现在上面的代码适用于单个选项卡,但我想知道有没有办法使用 Excel VBA 同时更改所有四个选项卡?

Thanks.

谢谢。

回答by danielpiestrak

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.Worksheetsto wbBK2.Worksheets, but I'm running too low on caffeine to remember, or time to test it.

我认为您可以Application.Worksheets改为wbBK2.Worksheets,但我的咖啡因太少,无法记住,也没有时间进行测试。

回答by Scott Holtzman

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

起初我认为使用该Sheets(Array("Sheet1","Sheet2")).Select方法会起作用,但经过测试(并运行宏记录器),我发现它没有。

Update

更新

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

ooo 发表了很棒的评论。这是更干净,更容易阅读的方式:

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