djskj189 djskj189 - 8 months ago 48
C# Question

Excel VBA macro color code

My program automatically highlights (fills color) of the excel cells based on inputs. I recorded Excel Macro to find the color code I want (integer value of the color; i.e. 16777215 for white), and I noticed that yellow is 65535 and cyan is 16776960 although in real life these two values should be reversed (yellow is 16776960 or ffff00 in RGB, and cyan is 65535 or 00ffff in RGB)

Could someone explain this behaviour please?


Answer Source

Probably here is the answer: Why are Excel RGB values backwards?

I have had the same problem some time ago, thus I have built something like this:

Option Explicit
'RGB2HTMLColor html htmlcolor
'INPUT: Numeric (Base 10) Values for R, G, and B)
'String to be used for color of element in VBA.
'E.G -> if the color is like this:-> &H80000005&
'we should change just the last 6 positions to get our color! H80 must stay.

Public Function RGB2HTMLColor(B As Byte, G As Byte, R As Byte) As String

    Dim HexR As Variant, HexB As Variant, HexG As Variant
    Dim sTemp As String

    On Error GoTo ErrorHandler

    HexR = Hex(R)
    If Len(HexR) < 2 Then HexR = "0" & HexR

    'Get Green Hex
    HexG = Hex(G)
    If Len(HexG) < 2 Then HexG = "0" & HexG

    HexB = Hex(B)
    If Len(HexB) < 2 Then HexB = "0" & HexB

    RGB2HTMLColor = HexR & HexG & HexB
    Debug.Print "Enter RGB, without caring for the real colors, the function knows what it is doing."
    Debug.Print "IF 50D092 then &H0050D092&"

    Exit Function

    Debug.Print "RGB2HTMLColor was not successful"
End Function