Range.Interior.Color Different Between Excel 2007 and Later

风流意气都作罢 提交于 2019-12-07 11:19:41

问题


I'm seeing that Range.Interior.Color returns different numbers for the same color in some cases, depending on whether it is running in Excel 2007, or Excel 2010 or 2013.

Is that expected?? I'm surprised.

Range.Interior.Color is the background color ("Fill Color") of the cell. In the Immediate pane, you can read it like this:

?ActiveCell.Interior.Color

And set it like this:

ActiveCell.Interior.Color = 10921638

Examples:

Example 1:

(these are the same color though their Range.Interior.Color are different.)

  • Excel 2007: 10855845
  • Excel 2010/2013: 10921638

Example 2:

  • Excel 2007: 14922893
  • Excel 2010/2013: 14857357

Example 3:

  • Excel 2007: 14211288
  • Excel 2010/2013: 14277081

Any suggestions? For now, I'm using conditional compliling to set constants for one number or the other depending on VBA constant VBA7, which returns True for Excel 2010 or later and False for Excel 2007 and earlier:

#If VBA7 Then
    'Excel 2010 or later:
    Const NO_SHADING_COLOR As Long = 16777215
    Const MAIN_HEADER_COLOR As Long = 10921638 'dark gray [in xl2007 s/b 10855845]
    Const SUB_HEADER_COLOR As Long = 14857357 'light blue [in xl2007 s/b 14922893]
    Const SUBSUB_HEADER_COLOR As Long = 14277081 'medium gray [in xl2007 s/b 14211288]
#Else
    'Excel 2007 or earlier:
    Const NO_SHADING_COLOR As Long = 16777215
    Const MAIN_HEADER_COLOR As Long = 10855845 'dark gray
    Const SUB_HEADER_COLOR As Long = 14922893 'light blue
    Const SUBSUB_HEADER_COLOR As Long = 14211288 'medium gray
#End If

UPDATE:

Yes, I understand that RGB can be used intead of Range.Interior.Color, and that RGB numbers can be extracted from Range.Interior.Color. But we can do that all day long and still get different sets of RGB numbers depending on Excel version, which effectively takes us back to the original problem.

For any given Range.Interior.Color number, yes it is equivalent to a certain set of RGB numbers. But the point is that depending on Excel version, in some cases you get different Range.Interior.Color numbers for the very same cell without changing the cell's color. If you extract that number to RGB numbers, then you just have different sets of RGB numbers depending on Excel version, which is no better than having the different Range.Interior.Color numbers depending on version.

These cell colors are being set by the user, using Excel's user interface for setting the cell's "Fill Color". The VBA code in this proejct does not set the colors. The VBA code only GETS the colors, and branches according to the color found in the cell being processed.

Aside from obvious exceptions like Application.Version, it isn't normal for object properties to randomly change like this from version to version. Consistency of object properties between versions is an important part of what allows VBA code to work across different versions. If it were not so, we'd have to conditional-compile virtually all our Excel VBA code.


回答1:


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?




回答2:


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).




回答3:


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).




回答4:


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.



来源:https://stackoverflow.com/questions/28071931/range-interior-color-different-between-excel-2007-and-later

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