Is there really no built-in worksheet function to convert RGB values to their hexadecimal equivalent?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,535
Office Version
  1. 365
Platform
  1. Windows
I cannot find a worksheet function to convert RGB values to their hexadecimal equivalents. Is that correct or did I miss something?

So I wrote this little UDF:

VBA Code:
Function RGB2Hex(R As Long, G As Long, B As Long) As Variant

RGB2Hex = Application.WorksheetFunction.Dec2Hex(R) _
        & Application.WorksheetFunction.Dec2Hex(G) _
        & Application.WorksheetFunction.Dec2Hex(B)
       
End Function

Is there something better?
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
You can always use VB's built-in Hex function to simplify your function. If you are absolutely sure you want the hex values concatenated as you show, your function can be simplified to this...
VBA Code:
Function RGB2Hex(ByVal R As Long, ByVal G As Long, ByVal B As Long) As Variant
  RGB2Hex = Hex(R) & Hex(G) & Hex(B)
End Function
Note I added "ByVal" in front of each of your function arguments... this avoids your function erroring out under certain conditions.

Now, you may be wondering why I wrote "If you are absolutely sure you want the hex values concatenated as you show". It has been a long while since I had to play with color values, but my recollection is that color hex values are concatenated in reverse order (B, G, R) . If I am correct (perhaps someone more knowledgeable about this will come along and clarify it for us), then this function, which does that, may be what you actually need...
VBA Code:
Function RGB2Hex(ByVal R As Long, ByVal G As Long, ByVal B As Long) As Variant
  RGBtoHEX = Hex(RGB(R, G, B))
End Function
 
Upvote 0
Maybe
Code:
Function HexFromRGB(Red As Long, Green As Long, Blue As Long) As String

HexFromRGB = "#" & Right("00" & Hex(Red), 2) & _
    Right("00" & Hex(Green), 2) & Right("00" & Hex(Blue), 2)

End Function
Should give FFD700 for Red = 255, Green = 215, Blue = 0, I think the functions previously give FFD70.

or formula wise

Book1
ABC
12552150
2
3
4#FFD700
Sheet9
Cell Formulas
RangeFormula
A4A4="#" & DEC2HEX((A1*65536)+(B1*256)+C1,6)
 
Last edited:
Upvote 0
How about this inline code for when R, G, & B are in E6, F6, & G6?

VBA Code:
="#"&TEXT(DEC2HEX(E6),"00") & TEXT(DEC2HEX(F6),"00") & TEXT(DEC2HEX(G6),"00")

It seems to work. It gives the correct result in Mark's example above.
 
Upvote 0
How about this inline code for when R, G, & B are in E6, F6, & G6?

VBA Code:
="#"&TEXT(DEC2HEX(E6),"00") & TEXT(DEC2HEX(F6),"00") & TEXT(DEC2HEX(G6),"00")
If that is really the order you actually want the result in (if you haven't seen it yet, see Message #2), then this simpler formula should do the same thing...

="#"&DEC2HEX(65536*E6+256*F6+G6)
 
Upvote 0
Solution

Forum statistics

Threads
1,215,428
Messages
6,124,832
Members
449,190
Latest member
rscraig11

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