vba Excel:Interior.ColorIndex 为相同的索引生成不同的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12031051/
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
Excel: Interior.ColorIndex producing different values for same index
提问by Swiftslide
I want to set a range of cells to have a grey interior. The grey I want comes right after Black and White on the pallete (top row, third from the left).
我想将一系列单元格设置为具有灰色内部。我想要的灰色紧跟在调色板上的黑色和白色之后(顶行,左起第三个)。
I set a cell's interior to this colour manually, then used vba to MsgBox the ColourIndex. It came up with 19. However, when i set a cell's Interior.ColorIndex property to equal 19, the cell is not grey but an off-white/cream colour.
我手动将单元格的内部设置为这种颜色,然后使用 vba 来 MsgBox ColourIndex。结果是 19。但是,当我将单元格的 Interior.ColorIndex 属性设置为等于 19 时,该单元格不是灰色而是灰白色/奶油色。
So much that the following code actually changes a cell's (cell A1's) color from grey to cream:
如此之多以至于以下代码实际上将单元格(单元格 A1 的)颜色从灰色更改为奶油色:
Dim r As Range
Set r = Range("A1")
Dim n As Integer
n = r.Interior.ColorIndex
r.Interior.ColorIndex = n
Can anyone explain this?
谁能解释一下?
采纳答案by RocketDonkey
I believe ColorIndex
only supports a limited numberof colors. Try this instead:
我相信ColorIndex
只支持有限数量的颜色。试试这个:
Dim r As Range
Dim n As Long ' The return values are large, so Integer will overflow
Set r = Range("A1")
' The color is 14806254
n = r.Interior.Color
r.Interior.Color = n
Or for your actual purpose:
或者为了您的实际目的:
Dim myRange As Range
Dim n As Long
myRange = Range("A1:F1")
n = 14806254
' Color away!
For Each cell In myRange
cell.Interior.Color = n
Next cell
回答by Jon Crowell
You also need to set the themecolor.
您还需要设置主题颜色。
Dim r As Range
Set r = Range("A1")
Dim n As Integer
n = r.Interior.ColorIndex
With r.Interior
.ColorIndex = n
.ThemeColor = xlThemeColorDark2
End With