简体   繁体   中英

Conditional formatting in Excel with HEX/ RGB value looked up in another table

On one sheet I've a color table with a range of values and associated HEX and RGB values. On the other sheet is a data table with a column with values within the range of values in the color table.

I'm trying to set the background color of the cells in the data table automatically when entered using the HEX/ RGB value looked up in the color table, without success.

My actual problem is, how to set the fill color of the conditional formatting with a HEX/ or RGB value.

Can anybody help me with this? enter image description here

Option 1: One time conditional formatting Create a new conditional format on cells having value = 3,5,7 and so on. Choose Color for it

Option 2: Loop through each cell and do a if or select case statement. Below is an example for color 3

For i = 2 To 3
Select Case Range("H" & i).Value

Case 3
    Range("H" & i).Interior.Color = RGB(Cells(i, 3), Cells(i, 4), Cells(i, 5))
End Select

End Select

Since none of the above answers did take account of my stated requirements

“… set the background color of the cells in the data table automatically when entered using the HEX/ RGB value looked up in the color table …”

I've worked out a solution for me, which is meeting my requirements.

Maybe this could also be of help for somebody else, so I'm putting the code below and also attach an Excel file, so you can test it.

VBA code “Worksheet_Change” on sheet “Data”

Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim rng_Trigger_Cat1 As Range   ' Category 1 Range of cells triggering this code and changing its backround color
    Dim rng_Trigger_Cat2 As Range   ' Category 2 Range of cells triggering this code and changing its backround color
    Dim crng As Range               ' Variable for applicable color range for triggering cell
    Dim rng As Range
    Dim HEX As String
    
    On Error Resume Next
        
    Application.EnableEvents = False
    
    Set rng_Trigger_Cat1 = Range("tbl_Data[Cat1]")
    Set rng_Trigger_Cat2 = Range("tbl_Data[Cat2]")
    
    HEX = ""

    If Not Application.Intersect(rng_Trigger_Cat1, Range(Target.Address)) Is Nothing Then
        Set crng = Worksheets("Colors").Range("tbl_Colors_Cat1")
    
    ElseIf Not Application.Intersect(rng_Trigger_Cat2, Range(Target.Address)) Is Nothing Then
        Set crng = Worksheets("Colors").Range("tbl_Colors_Cat2")
    
    End If
       
    For Each rng In Target
 
        ' Get HEX code for specified color of cell value and category from color tables on sheet "Colors" and set backround color of input cell.
        ' If input value is not specified in according color table, the color of the cell will be set to "Blank".
        HEX = Right(Application.WorksheetFunction.VLookup(Target, crng, 3, False), 6)

        If Len(HEX) = 6 Then
            rng.Interior.Color = _
              RGB(Application.Hex2Dec(Left(HEX, 2)), _
                  Application.Hex2Dec(Mid(HEX, 3, 2)), _
                  Application.Hex2Dec(Right(HEX, 2)))
        Else
            rng.Interior.Pattern = xlNone
        End If
        
    Next rng
    
    Application.EnableEvents = True

End Sub

With this solution you can also add additional values “#” and related colors to the tables or change existing color specifications on the Colors sheet and have them automatically available in the according category column on the Data sheet.

There is also a button on the Colors sheet, whose code is repairing the existing coloring in the Data table after changes to the tables on the Collors sheet have been applied.

VBA code cmdButton “Fix Colors …”

Private Sub cmd_FixColors_Click()

    Dim rng As Range
    Dim cell As Range
    
    Set rng = Worksheets("Data").Range("tbl_Data[Cat1],tbl_Data[Cat2]")
    
    For Each cell In rng
        cell = cell.Value
    Next cell

End Sub

To avoid values within a category in the Data table without a specified color you can use Data Validation with the category-color tables as source, as shown in the sample file.

Feedback and suggestions are always welcome!

Excel Sample File

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM