Excel 2007 conditional formatting - how to get cell color?

后端 未结 7 1296
别那么骄傲
别那么骄傲 2020-12-06 02:17

Let\'s assume i have the following range from (a1:c3)

  A B C
1 -1 1 1
2 -1 0 0
3  0 0 1

Now i have selected the following range, and forma

7条回答
  •  清歌不尽
    2020-12-06 02:19

    .Interior.Color returns the "real" color, not the conditionally-formatted color result.

    @sss: It's not available via the API.

    The best you can do is to test the same conditions you used in the conditional formatting.

    To avoid this resulting in duplicate code, I suggest moving your conditional criteria to a UDF. Examples:

    Function IsGroup1(ByVal testvalue As Variant) As Boolean
       IsGroup1 = (testvalue < 0)
    End Function
    
    Function IsGroup2(ByVal testvalue As Variant) As Boolean
       IsGroup1 = (testvalue = 0)
    End Function
    
    Function IsGroup3(ByVal testvalue As Variant) As Boolean
       IsGroup1 = (testvalue > 0)
    End Function
    

    Then use these formulas in your Conditional formatting:

    =IsGroup1(A1)
    =IsGroup2(A1)
    =IsGroup3(A1)
    

    Then your code, rather than looking at the color of the cells, looks to see if the condition is met:

    If IsGroup1(Range("$A$1").Value) Then MsgBox "I'm red!"
    

提交回复
热议问题