vba 更改整个工作表的背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13008714/
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:15:47  来源:igfitidea点击:

Change Background Color for Entire Sheet

excelvbabackground-color

提问by Adrian

Is there a way to change the Background color into xlNone, for example to the entire sheet. I see that you can put a background image... But how can you change the color for all cells in the sheet?

有没有办法将背景颜色更改为xlNone,例如更改为整个工作表。我看到您可以放置​​背景图像...但是如何更改工作表中所有单元格的颜色?

回答by Jook

You can do this quite easy with this code:

您可以使用以下代码轻松完成此操作:

Public Sub Demo()
  'set color
  WorksheetName.Cells.Interior.ColorIndex = 1 'black
  'clear color
  WorksheetName.Cells.Interior.ColorIndex = xlColorIndexNone
end sub

Or if you don't need VBA, just click at this little icon in the corner:

或者,如果您不需要 VBA,只需单击角落中的这个小图标:

enter image description here

在此处输入图片说明

and select a color, or to use no color - using right click menu or ribbon menu.

并选择一种颜色,或者不使用颜色 - 使用右键菜单或功能区菜单。

Just because of the other answers, I want to remind - it is not necessary to use selections! This is bad macro-recorder style. There are only few occations, where using selections is necessary or a good idea. You can always just use a specific range.

只是因为其他答案,我想提醒 - 没有必要使用选择!这是糟糕的宏记录器风格。只有少数情况下,使用选择是必要的或一个好主意。您始终可以只使用特定范围。

回答by CustomX

Try this

尝试这个

Sheets("Sheet1").Select    
Cells.Select
With Selection.Interior
    .Pattern = xlNone
    .TintAndShade = 0
    .PatternTintAndShade = 0
End With

回答by Ahmad

Here is how you can change the background color for all cells in the current sheet

以下是如何更改当前工作表中所有单元格的背景颜色

 Cells.Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = -0.14996795556505   'grey color
        .PatternTintAndShade = 0
    End With