Range.Interior.Color Different Between Excel 2007 and Later

冷暖自知 提交于 2019-12-05 14:01:57

If you search for “Excel colour codes” you will find sites that list Excel’s 56 “official” colours. You will not find any of numbers you list in the official list.

The Red, Green and Blue values for the numbers you list are:

             Red   Green   Blue 
10855845     165     165    165
10921638     166     166    166
 9868950     150     150    150  Grey 40%

14922893     141     180    227
14857357     141     180    226
16764057     153     204    255  Pale blue

14211288     216     216    216
14277081     217     217    217
12632256     192     192    192  Grey 25%   

In each sub-table, the first two lines show your colour numbers. Although the combined numbers like very different, you will notice the separate values are almost identical so the two alternative colours would look the same on the screen. The third line shows the nearest official Excel colour and its name.

The question for you to investigate is: where have these non-standard colour numbers come from? Are these colours being set by a program or has the palette been very subtly changed by a user? I find it difficult to believe Microsoft released Excel 2007, 2010 and 2013 to you in this state.

New section

I have been playing with Excel colours and I am convinced your problem is related to the palette and the use of non-standard colours.

I do not have your range of versions so experimented with 2003 and 2007.

With 2003 there is a palette of 56 colours and a colour index with a value of 1 to 56. I believe that Excel stores the colour index against a cell because:

  • If with VBA you set a cell to an off-palette colour, Excel immediately changes it to an on-palette colour.
  • If you change a colour on the palette, every use of that colour within the workbook immediately changes.

That is, with 2003 you can have any 56 colours but only 56 different colours.

Under 2003, I created a new workbook. On the palette, I switched Red and Blue and I replaced Indigo, RGB(51, 51, 163), by RGB(167, 167, 167). This is not a standard Excel colour but, if it was, it would be named something like Grey 30. I coloured a few cells using these colours.

I opened this workbook with 2007 and saved it as an xlsm file. The colours on the worksheet appeared unchanged. I selected a new cell and defined a custom colour of RGB(167, 167, 167). Visually the new cell was identical (to my eyes) to those coloured with the RGB(167, 167, 167) imported from 2003. I then examined these cells via the palette and VBA:

Colour I    Appearance   Number according      Number according
had set     on screen    to palette            to VBA
255,0,0       Red        0,0,255 Blue          255,0,0
0,0,255       Blue       255,0,0 Red           0,0,255
167,167,167   Grey 30    51,51,153 Indigo      167,167,167      Imported from 2003
167,167,167   Grey 30    167,167,167 Grey 30   167,167,167      Created with 2007

The implication from the last line is that 2007 can handle custom colours correctly if defined within 2007 but gets hopelessly confused with imported custom colours.

My understanding is that 2007 to 2010 to 2013 involved incremental improvements but 2003 to 2007 was a total rewrite. I would guess a bug in 2007 was fixed in 2010.

I believe the problem is the use of non-standard colours. 2007 may have a larger standard palette but RGB(167, 167, 167) is not on it. I do not have 2010 or 2013 but suspect I would get different results if I tried this experiment with either of them.

I believe you must re-colour all cells that use these custom colours with standard colours. You report two custom shades of grey and a custom variation of pale blue. Surely 2007, 2010 and 2013 offer enough standard shades and colours?

Update:

I haven't been able to reproduce this problem in a new workbook, so it may be workbook corruption. Normally Range.Interior.Color is reliable across versions, for all colors represented.

FWIW, this workbook was sent to me by another person, and that person is on Excel for Macintosh while I am on Windows, so creating the workbook on one platform and then using it on another may have been a factor in the corruption, if any (even though that should work fine).

I have been doing some work with a project and come across something that may help with this whole question. I have been working on Excel 2010 (and don't have access to any more versions at the moment), but found out that there is more than one way to refer to 3 x 8-bit colours. While we are all familiar with RGB (Red-Green-Blue) references I came across someone using BGR (Blue-Green-Red) and got very confused in a similar manner to the original post. I remember doing work a long time ago on XL2003 and found that the colours were "correct", so I suspect that MS have changed the references without telling everybody (their documentation states RGB).

You can use the RGB() function instead of those numbers, as for example in

   Activecell.Interior.Color = RGB(200,150,230)

If you would like the constants, then

    Const RED = 200
    Const GRN = 150
    Const BLU = 230
    ......
    Activecell.Interior.Color = RGB(RED, GRN, BLU)

As for "Is it expected?", here is this: Color = 10921638 is RGB(165, 165, 165) while Color = 10855845 is RGB(166, 166, 166). I have no access to Excel 2007 at the moment, but if your color values are indeed system's "dark grey" in both cases then Microsoft has a changed the value for "dark grey" between the Excel versions. I suspect you do not care or able to see the difference between the two anyway... You might consider using the same color in all versions, I think.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!