Results 1 to 10 of 15

Thread: Get Displayed Cell Color (whether from Conditional Formatting or not)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    659
    Rep Power
    13

    Get Displayed Cell Color (whether from Conditional Formatting or not)

    How can I tell what color the cell is? This question seems to come up quite often in the various forums and newsgroups that I visit. Almost universally, the person posing the question gets asked "Is the cell's color due to Conditional Formatting or not?" The reason? VB does does not provide direct access to the color displayed in a cell. So, I decide to try my hand at writing code to return the displayed color no matter how that color got into the cell. The following code (which I believe works correctly) is the result of that effort...

    SEE CODE UPDATE IN NEXT MESSAGE
    Code:
    ' Arguments
    ' ----------------
    ' Cell - Required Range, not a String value, for a **single** cell
    '
    ' CellInterior - Optional Boolean (Default = True)
    '                True makes function return cell's Interior Color or ColorIndex based on
    '                the ReturnColorIndex argument False makes function return Font's Color or
    '                ColorIndex based on the ReturnColorIndex argument
    '
    ' ReturnColorIndex - Optional Boolean (Default = True)
    '                    True makes function return the ColorIndex for the cell property determined
    '                    by the CellInterior argument False make function return the Color for the
    '                    cell property determined by the CellInterior argument
    '
    Function DisplayedColor(Cell As Range, Optional CellInterior As Boolean = True, _
                            Optional ReturnColorIndex As Long = True) As Long
      Dim X As Long, Test As Boolean, CurrentCell As String
      If Cell.Count > 1 Then Exit Function
      CurrentCell = ActiveCell.Address
      For X = 1 To Cell.FormatConditions.Count
        With Cell.FormatConditions(X)
          If .Type = xlCellValue Then
            Select Case .Operator
              Case xlBetween:      Test = Cell.Value >= Evaluate(.Formula1) And Cell.Value <= Evaluate(.Formula2)
              Case xlEqual:        Test = Evaluate(.Formula1) = Cell.Value
              Case xlGreater:      Test = Cell.Value > Evaluate(.Formula1)
              Case xlGreaterEqual: Test = Cell.Value >= Evaluate(.Formula1)
              Case xlLess:         Test = Cell.Value < Evaluate(.Formula1)
              Case xlLessEqual:    Test = Cell.Value <= Evaluate(.Formula1)
              Case xlNotBetween:   Test = Cell.Value <= Evaluate(.Formula1) Or Cell.Value >= Evaluate(.Formula2)
              Case xlNotEqual:     Test = Evaluate(.Formula1) <> Cell.Value
            End Select
          ElseIf .Type = xlExpression Then
            Application.ScreenUpdating = False
            Cell.Select
            Test = Evaluate(.Formula1)
            Range(CurrentCell).Select
            Application.ScreenUpdating = True
          End If
          If Test Then
            If CellInterior Then
              DisplayedColor = IIf(ReturnColorIndex, .Interior.ColorIndex, .Interior.Color)
            Else
              DisplayedColor = IIf(ReturnColorIndex, .Font.ColorIndex, .Font.Color)
            End If
            Exit Function
          End If
        End With
      Next
      If CellInterior Then
        DisplayedColor = IIf(ReturnColorIndex, Cell.Interior.ColorIndex, Cell.Interior.Color)
      Else
        DisplayedColor = IIf(ReturnColorIndex, Cell.Font.ColorIndex, Cell.Font.Color)
      End If
    End Function
    Since I was the writing the code, I could not resist adding some extra functionality. I did this by including two optional arguments. The first, CellInterior, is a Boolean which controls whether the returned value is the color of the cell's interior or the cell's font. The default value is True which means the function returns the color of the cell's interior by default. The second, ReturnColorIndex, is also a Boolean and it controls whether the returned value is the ColorIndex or the RGB Color value. The default value is True which means the function returns the ColorIndex by default.

    Unfortunately, this function cannot be used as a UDF (user defined function). The reason behind this will also help you understand why the code in the block underneath this statement looks the way it does...

    Code:
    ElseIf .Type = xlExpression Then
    It seems that Excel uses the active cell to figure out relative cell references for a specified cell when returning the Formula1 property when a Conditional Format is using a formula rather than a cell value relation. This had me going for quite awhile as sometimes the code worked and other times it didn't. Finally, I noticed the discrepencies were related to what cell was active (the cell with the Conditional Format or some other cell). Because of that, it is necessary to activate the cell being examined before asking it for its formula. So, this need to activate the cell is what stops this function from being able to be used as a UDF.

    *** NOTE: See Updated Code In My Reply To This Message ***


    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA

    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    https://www.eileenslounge.com/viewtopic.php?f=30&t=40533&p=314837#p314837
    https://www.eileenslounge.com/viewtopic.php?f=21&t=40701&p=314836#p314836
    https://www.eileenslounge.com/viewtopic.php?f=27&t=40621&p=314621#p314621
    https://www.eileenslounge.com/viewtopic.php?f=27&t=40621&p=314619#p314619
    https://www.eileenslounge.com/viewtopic.php?f=27&t=40621&p=314600#p314600
    https://www.eileenslounge.com/viewtopic.php?f=27&t=40621&p=314599#p314599
    https://www.eileenslounge.com/viewtopic.php?f=27&t=40621&p=314274#p314274
    https://www.eileenslounge.com/viewtopic.php?f=27&t=40621&p=314229#p314229
    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    Last edited by DocAElstein; 03-06-2024 at 02:43 PM.

Similar Threads

  1. Conditional formatting
    By mahmoud-lee in forum Excel Help
    Replies: 9
    Last Post: 05-30-2013, 03:00 PM
  2. Conditional Formatting using formula.
    By Ryan_Bernal in forum Excel Help
    Replies: 2
    Last Post: 02-18-2013, 11:33 PM
  3. Conditional Formatting - If/And Formula
    By Katrina in forum Excel Help
    Replies: 4
    Last Post: 11-23-2012, 12:45 AM
  4. Replies: 2
    Last Post: 09-16-2012, 02:28 AM
  5. Replies: 4
    Last Post: 07-27-2012, 08:43 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •