重点内容
适用版本
桌面版通用(Excel 365 / 2021 / 2019 等)。
用 ColorIndex 设置背景色
Range("A1").Interior.ColorIndex = 37ColorIndex 是 Excel 内建的 56 种颜色编号,37 号是浅蓝色。
运行代码后 A1 单元格背景变成浅蓝色的效果
清除背景色
Range("A1").Interior.ColorIndex = 0设成 0 代表「无填充」。
查询目前的颜色编号
MsgBox Selection.Interior.ColorIndex用 RGB 自由调色
Range("A1").Interior.Color = RGB(255, 0, 0)ColorIndex 只能选 56 种固定颜色,想要更精确的颜色要改用 Color 属性搭配 RGB() 函数。
方法怎么选
| 方法 | 适合场景 | 备注 |
|---|---|---|
Interior.ColorIndex | 用 Excel 内建的 56 种预设色就够用 | 吃编号数字(0 代表无填充) |
Interior.Color + RGB() | 想要更精确、自由调色 | 吃 RGB 或色码,不能跟 ColorIndex 混用 |
学完你会
常见错误
- 混用
ColorIndex和Color两个不同属性——ColorIndex吃编号数字,Color吃 RGB 或色码,两者不能互相代入 - 想清除背景色却设成别的数字而不是 0,格子还是留着某种颜色
- 把
Interior(背景)跟 EP09 的Font(字体)搞混,改错了该改的属性
Sources
Blog / Website: