VBA Excel: Range of RGB for Colorindex compatibility

对着背影说爱祢 提交于 2019-12-13 03:05:50

问题


I have a macro created in Excel 2007 which lets a value depend on the font color of a source file, as follows (this piece of code is part of a loop):

 If Worksheets("Source1").Cells(i, j).Font.Color = RGB(165, 165, 165) Or Worksheets("Source1").Cells(i, j).Font.Color = RGB(117, 146, 60) Then
     Worksheets("Result").Cells(UnusedRow, 15).Value = "Closed"
 Else
     Worksheets("Result").Cells(UnusedRow, 15).Value = "Active"
 End If

This works without a problem.

However, strangely enough the same cells in the same source file have different RGB values when opened with Excel 2013: RGB(165, 165, 165) in 2007 becomes RGB(166, 166, 166) in 2013, and RGB(117, 146, 60) in 2007 becomes RGB(118, 147, 60) in 2013.

That is why I thought to use a small range for every number in the font color, for instance for RGB(x, y, z) from the source file:

 If x => 164 And x <= 167 And y => 164 And y <= 167 And z => 164 And z <= 167 Then 
   ...

Can someone tell me how to code this correctly? Thanks!


回答1:


Okay, my brain finally came up for air, and I've got an answer.

Instead of Color, use ColorIndex. That's the index of the position of the color they pick in the palette.

I'm not saying it's a great system (it's not) but given your constraint - relying on the user to click a certain color button, it's as good as you're going to get.

I think.



来源:https://stackoverflow.com/questions/27173644/vba-excel-range-of-rgb-for-colorindex-compatibility

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