PDA

View Full Version : [SOLVED:] Hex Colors Display in Excel Wrong - RGB



dj44
02-27-2017, 05:54 AM
Hi folks,:)

I am having some rgb hex trouble



This hex color is gray #C7C7C7 -

- but it displays as green - a lot of colors are displayed wrong as well from the hex side.

I am testing the display color in excel.

I read somewhere that excel displays it as BGR






Sub DisplayHexColorsinCell()

'Converts Hex to RGB Colors - Display Color in Column E Hex numbers in column D

Dim i, LastRow
LastRow = Range("D" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
Cells(i, "E").Interior.Color = HEXCOL2RGB(Cells(i, "D"))
Next
End Sub



Public Function HEXCOL2RGB(ByVal HexColor As String) As String
Dim Red As String, Green As String, Blue As String
HexColor = Replace(HexColor, "#", "")
Red = Val("&H" & Mid(HexColor, 1, 2))
Green = Val("&H" & Mid(HexColor, 3, 2))
Blue = Val("&H" & Mid(HexColor, 5, 2))


'HEXCOL2RGB = RGB(Red, Green, Blue)

HEXCOL2RGB = "(" & Red & "," & Green & "," & Blue & ")"
End Function





i found many hex colors here to test
http://cloford.com/resources/colours/500col.htm

Apparantly it displays in excel as BGR, and I cant work out what this means

I just want to display the correct hex color in the cell so i know its the right color as i saw on the web

GTO
02-27-2017, 07:31 AM
The .Color property is basically BGR. I'm not sure if this will help you 'see' how that works, but in a blank/new workbook, step thru this code and maybe it will demonstrate?



Sub example()

Cells(2, 1) = "AA"
Cells(3, 1) = "BB"
Cells(4, 1) = "CC"
Cells(2, 2).Resize(3).Formula = "=HEX2DEC(A2)"
Cells(2, 3).Interior.Color = RGB(Cells(2, 2), Cells(3, 2), Cells(4, 2))

MsgBox Hex(Cells(2, 3).Interior.Color)

End Sub

Paul_Hossler
02-27-2017, 07:33 AM
.Color is a Long, as is the output of RGB()





Option Explicit
'Converts Hex to RGB Colors - Display Color in Column E Hex numbers in column D
Sub DisplayHexColorsinCell()

Dim i, LastRow
LastRow = Range("D" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
Cells(i, "E").Interior.Color = HEXCOL2RGB(Cells(i, "D"))
Next
End Sub



Public Function HEXCOL2RGB(ByVal HexColor As String) As Long
Dim Red As String, Green As String, Blue As String

HexColor = Replace(HexColor, "#", "")
Red = Val("&H" & Mid(HexColor, 1, 2))
Green = Val("&H" & Mid(HexColor, 3, 2))
Blue = Val("&H" & Mid(HexColor, 5, 2))

HEXCOL2RGB = RGB(Red, Green, Blue)
End Function

dj44
02-27-2017, 10:29 AM
Thanks Paul,

and other folk.

I dont what you did but the color corrected now with this new revision.

I got a bit too ambitious with coloring my excel cells and they went all pear shaped.

Because the hex color was all wrong but i didnt know that untill i tested it today.

so i got to fix all those spreadsheets now.

I had no idea untill i opened my spreadsheets and saw these horrible colors
that really gave me anxiety

oh well
thanks for the fix upper

Great day to all :)