2

I want to determine the color of a current cell. When I

Debug.Print ActiveCell.Font.Color

I get back a number. In this case 24832
How do I determine the proper RGB value of this color given that I only have this number?
Is there some function that turns an int into RGB(0,0,0)?

Community
  • 1
  • 1
Ken Ingram
  • 1,538
  • 5
  • 27
  • 52

2 Answers2

5

There is no built-in VBA conversion to RGB, you will need to write your own.

Try This

Function ColorToRgb(Color As Variant) As Variant
    ' return #Value Error for invalid colors
    If Color < 0 Or Color > 16777215 Then
        ColorToRgb = CVErr(xlErrValue)
    End If

    ReDim Res(1 To 3) As Variant
    ' Treating Color as a 24 bit number
    Res(1) = Color Mod 256          ' Red value: left most 8 bits
    Res(2) = Color \ 256 Mod 256    ' Green value: middle 8 bits
    Res(3) = Color \ 65536 Mod 256  ' Blue value: right most 8 bits

    ColorToRgb = Res
End Function
chris neilsen
  • 52,446
  • 10
  • 84
  • 123
2

This is a much quicker bit mask for RGB components:

Function RGBComp(Clr As Variant) As Variant
    If Clr >= 0 Or Clr <= 16777215 Then
        RGBComp = Array(Clr And &HFF, (Clr And &HFF00&) \ &H100&, (Clr And &HFF0000) \ &H10000)
    Else
        RGBComp = CVErr(xlErrValue)
    End If
End Function
Excel Hero
  • 14,253
  • 4
  • 33
  • 40