VBA to return RGB Color Code

chingching831

New Member
Joined
Jun 2, 2022
Messages
32
Office Version
  1. 2019
Platform
  1. Windows
Hi there,

May I know if there's a VBA Code that can return the RGB colour code of a coloured cell?

1718518533512.png


Thanks in advance!

Best,
SC
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Colors starting at A2 on down.
Code:
Sub Maybe()
Dim i As Long, N As Long, R As Long, G As Long, B As Long
For i = 2 To 6    '<---- 31 in your example
    With Cells(i, 1)
        N = .Interior.Color
        B = Int(N / 65536)
        G = Int(((N / 65536) - B) * 256)
        R = N - (B * 65536) - (G * 256)
    .Offset(, 1).Value = R
    .Offset(, 2).Value = G
    .Offset(, 3).Value = B
    .Offset(, 4).Value = N
    .Offset(, 5).Value = "RGB(" & R & ", " & G & ", " & B & ")"
    End With
Next i
End Sub
 
Upvote 1
Solution
Works well! Thanks a lot!
Colors starting at A2 on down.
Code:
Sub Maybe()
Dim i As Long, N As Long, R As Long, G As Long, B As Long
For i = 2 To 6    '<---- 31 in your example
    With Cells(i, 1)
        N = .Interior.Color
        B = Int(N / 65536)
        G = Int(((N / 65536) - B) * 256)
        R = N - (B * 65536) - (G * 256)
    .Offset(, 1).Value = R
    .Offset(, 2).Value = G
    .Offset(, 3).Value = B
    .Offset(, 4).Value = N
    .Offset(, 5).Value = "RGB(" & R & ", " & G & ", " & B & ")"
    End With
Next i
End Sub
 
Upvote 0
Colors starting at A2 on down.
Code:
Sub Maybe()
Dim i As Long, N As Long, R As Long, G As Long, B As Long
For i = 2 To 6    '<---- 31 in your example
    With Cells(i, 1)
        N = .Interior.Color
        B = Int(N / 65536)
        G = Int(((N / 65536) - B) * 256)
        R = N - (B * 65536) - (G * 256)
    .Offset(, 1).Value = R
    .Offset(, 2).Value = G
    .Offset(, 3).Value = B
    .Offset(, 4).Value = N
    .Offset(, 5).Value = "RGB(" & R & ", " & G & ", " & B & ")"
    End With
Next i
End Sub
Thank you!
 
Upvote 0
Yet another neat way that makes use of the little known vba LSET statement:

VBA Code:
Option Explicit

Private Type UDT_RGB
    r As Byte
    g As Byte
    b As Byte
End Type

Private Type UDT_LONG
    n As Long
End Type

Function LongToRGB(ByVal Col As Long) As UDT_RGB
    Dim uRGB As UDT_RGB, uDummy As UDT_LONG
    uDummy.n = Col
    LSet uRGB = uDummy
    With LongToRGB
        .r = uRGB.r: .g = uRGB.g:  .b = uRGB.b
    End With
End Function

Usage Example:
VBA Code:
Sub Test()
    'Copy Interior color from Cell 'A1' to Cell 'C1'
    Dim lCol As Long, uRGB As UDT_RGB
    lCol = Range("A1").Interior.Color
    uRGB = LongToRGB(lCol)
    With uRGB
        Debug.Print "Red: "; "&H"; Hex(.r), "Green: "; "&H"; Hex(.g), "Blue: "; "&H"; Hex(.b)
        Range("C1").Interior.Color = rgb(.r, .g, .b)
    End With
End Sub
 
Upvote 1

Forum statistics

Threads
1,221,831
Messages
6,162,252
Members
451,757
Latest member
iours

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