RGB codes

plettieri

Well-known Member
Joined
Sep 4, 2002
Messages
1,594
Platform
  1. MacOS
Hello all,

Looking to extract the RGB color codes from cells that have been either conditional formatted or paint brush with colors. Any suggestions?

Plettieri
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
You could try the following for cell colours by creating function and then use it on a sheet.

VBA Code:
Function getColor(Rng As Range, ByVal ColorFormat As String) As Variant
    Dim ColorValue As Variant
    ColorValue = Cells(Rng.Row, Rng.Column).Interior.Color
    Select Case LCase(ColorFormat)
        Case "index"
            getColor = Rng.Interior.ColorIndex
        Case "rgb"
            getColor = (ColorValue Mod 256) & ", " & ((ColorValue \ 256) Mod 256) & ", " & (ColorValue \ 65536)
        Case Else
            getColor = "Only use 'Index' or 'RGB' as second argument!"
    End Select
End Function

Sheet formula (unfortunately the addin has stopped working on my laptop)

=getColor(C3,"rgb")
=getColor(C3,"Index")


This is not my code check here:
 
Upvote 0
To get the colours of Conditional formatting you'll need to use DisplayFormat and a workaround to get the DisplayFormat to work in a UDF. Something like the adaptions in the codes below

VBA Code:
Function getColor(Rng As Range, ByVal ColorFormat As String) As Variant
    Dim ColorValue As Variant
 
    Application.Volatile
    ColorValue = Evaluate("myRGB(" & Cells(Rng.Row, Rng.Column).Address() & ")")
    Select Case LCase(ColorFormat)
    Case "index"
        getColor = Evaluate("myIndex(" & Rng.Address() & ")")
    Case "rgb"
        getColor = (ColorValue Mod 256) & ", " & ((ColorValue \ 256) Mod 256) & ", " & (ColorValue \ 65536)
    Case Else
        getColor = "Only use 'Index' or 'RGB' as second argument!"
    End Select
End Function

Private Function myRGB(ByVal myRng As Range) As Double
    myRGB = myRng.DisplayFormat.Interior.Color
End Function

Private Function myIndex(ByVal myRng2 As Range) As Double
    myIndex = myRng2.DisplayFormat.Interior.ColorIndex
End Function

Cell Formulas
RangeFormula
D2:D10D2=getColor($A2,"rgb")
E2:E10E2=getColor($A2,"Index")
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A2:A17Cell Value>28textNO


Edit:
Based on a post by @Jaafar Tribak (he got the idea from somewhere else, I'd have to find his post to credit the original poster)
 
Last edited:
Upvote 0
Solution

Forum statistics

Threads
1,224,084
Messages
6,176,270
Members
452,718
Latest member
Nyxs_Inquisitor

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top