2017-05-26 196 views
2

我有兩個單元格:綠色和紫色。我的代碼:Excel vba .Interior.ColorIndex無法正常工作

Sub test() 
    With Selection.Interior 
     MsgBox (.ColorIndex) 
    End With 
End Sub 

Somewhy的MsgBox與數15似乎都爲綠色和紫色的顏色。 當我使用的代碼

Sub test() 
    With Selection.Interior 
     MsgBox (.Color) 
    End With 
End Sub 

它給不同數量但對於綠色和紫色的顏色獨特:1021331613082801

爲什麼ColorIndex會給出相同的數字,爲什麼Color會給出不同的唯一數字?

無論如何,在代碼中使用.Color返回的數字安全嗎?它不會因電腦而改變嗎?

+0

FYI周圍'.Color'的括號是多餘的,最終會咬你的後端。 [看看爲什麼](https://stackoverflow.com/documentation/vba/1179/procedure-calls/3818/this-is-confusing-why-not-just-always-use-parentheses#t=201705261415301545264)。 –

回答

5

.Color是一個比.ColorIndex(它有更多的顏色定義)更廣泛的調色板。 ColorIndex必須近似以彌補更少的定義顏色和顏色陰影。

enter image description here