Excel 2007 conditional formatting - how to get cell color?

后端 未结 7 1310
别那么骄傲
别那么骄傲 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:25

    It doesn't appear that the "Conditional Format"-color is available programmatically. What I'd suggest that, instead, you write a small function that calculates cell color, and then just set a macro to run it on the active cell whenever you've edited the value. For example (sorry for the psuedo-code - I'm not a VBA expert anymore):

    Function GetColorForThisCell(Optional WhatCell as String) as Int
    
       If WhatCell="" Then WhatCell = ActiveCell
    
       If Range(WhatCell).value = -1 then GetColorForThisCell = vbGreen
       If Range(WhatCell).value =  0 then GetColorForThisCell = vbYellow
       If Range(WhatCell).value =  1 then GetColorForThisCell = vbRed
    End Function
    
    Sub JustEditedCell
       ActiveCell.color = GetColorForThisCell()
    End Sub
    
    Sub GetColorOfACell(WhatCell as string)
       Msgbox(GetColorForThisCell(WhatCell) )
    End Sub
    

    Though you wouldn't be able to use the built-in Excel Conditional Formatting, this would accomplish the same thing, and you'd be able to read the color from code. does this make sense?

提交回复
热议问题