Share via

RGB function in VBA returns an incorrect value

Anonymous
2025-06-22T18:05:58+00:00

System Information

Windows

Edition Windows 10 Home

Version 22H2

Installed on ‎12/‎16/‎2023

OS build 19045.5965

Experience Windows Feature Experience Pack 1000.19061.1000.0

Microsoft 365

Microsoft® Excel® for Microsoft 365 MSO (Version 2505 Build 16.0.18827.20102) 64-bit

Microsoft® Access® for Microsoft 365 MSO (Version 2505 Build 16.0.18827.20102) 64-bit

Summary

I've tested this on Excel and Access, with the same result. A call to the RGB function in the VBA library returns an incorrect result. The function appears to invert the red and blue values. This can be recreated with the code below.

Code Example

Public Sub sbTestRGB() 

    Dim lngColor As Long 

    Dim intRed As Integer 

    Dim intGreen As Integer 

    Dim intBlue As Integer 

    intRed = 100 

    intGreen = 255 

    intBlue = 200 

    Debug.Print "Hex Red: " & Hex(intRed) 

    Debug.Print "Hex Green: " & Hex(intGreen) 

    Debug.Print "Hex Blue: " & Hex(intBlue) 

    Debug.Print "Combined Hex: " & Hex(intRed) & Hex(intGreen) & Hex(intBlue) 

    Debug.Print "Hex to Long: " & CLng("&h" & Hex(intRed) & Hex(intGreen) & Hex(intBlue)) 

    lngColor = RGB(intRed, intGreen, intBlue) 

    Debug.Print "Result from RGB function: " & lngColor 

    Debug.Print "Long to Hex: " & Hex(lngColor) 

End Sub

Code Result

On my system the result is as follows. Note that when the long value from the RGB function is converted back to hex, the result has the red and blue values inverted compared with the original hex values.

Hex Red: 64

Hex Green: FF

Hex Blue: C8

Combined Hex: 64FFC8

Hex to Long: 6619080

Result from RGB function: 13172580

Long to Hex: C8FF64

Note that an online RGB calculator verifies that the correct numeric value for these RGB values is 6619080. An online calculator also verifies that with an input number of 13172580 (the result from the RGB function), the red value is 200, green is 255, and blue is 100.

Microsoft 365 and Office | Excel | For education | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

2 answers

Sort by: Most helpful
  1. HansV 462.6K Reputation points
    2025-06-22T19:11:23+00:00

    This is because the Red value is stored in the 4th byte of a Long (32-bits), the Green value in the 3rd byte and Blue value in the 2nd byte. The 1st byte is always 0 for standard RGB colors.

    Pure red is 000000FF

    Pure green is 0000FF00

    Pure blue is 00FF0000

    In other words, a color is actually stored as BGR, not as RGB.

    Was this answer helpful?

    2 people found this answer helpful.
    0 comments No comments
  2. Anonymous
    2025-06-22T20:33:40+00:00

    Thank you. It's interesting that the online tools I used to check the value calculated it in reverse. Maybe it depends on the platform.

    Was this answer helpful?

    0 comments No comments