RGB colors with Their respective values in one sheet macro

witsonjoyet

Board Regular
Joined
Sep 13, 2013
Messages
100
Hi,
Good Day,
Could anyone write a macro to show the all RGB colours (1.6 million) in one single sheet with a RGB number. I.e. if i place the cursor in a cell then it needs to show/pop-up the RGB value of a particular cell, like wise i need to show the entire 1.6 million RGB colours in one sheet (if possible).
Thanks in advance.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
In fact there are 16 million combinations. 256 * 256 * 256 = 16,777,216
I don't know how long it takes for Excel to do 16 million cycles.

Maybe you will have to pause every 256 cycles, save the file and continue. That will take longer, but if excel were to close, at least you will have a breakthrough and restart the macro from the point where it stayed, this is the initial values i = 1 and j = 1, you will have to change them by the row number and number column where the last value was written.

You must also start the for counters with the RGB values written in the last cell.

Try this
VBA Code:
Sub RGB_Colors()
  Dim r As Long, g As Long, b As Long, i As Long, j As Long
  i = 1
  j = 1
  For r = 0 To 255
    For g = 0 To 255
      For b = 0 To 255
        Cells(i, j).Interior.Color = RGB(r, g, b)
        Cells(i, j).Value = "RGB(" & r & "," & g & "," & b & ")"
        i = i + 1
      Next
      'ActiveWorkbook.Save 'Activate this line if you want to save the book every 256 cycles.
    Next
    j = j + 1
    i = 1
  Next
End Sub
 
Upvote 0
Hello witsonjoyet,

I don't know which version of Excel your are using but you will most likely encounter the error "Too many different cell formats". I am using Excel 2010 and the error occurred at 65,280 cells.
 
Upvote 0
I suggest just to use Step to get few results for demonstration only
VBA Code:
        For r = 0 To 255 Step 25
            For g = 0 To 255 Step 25
                For b = 0 To 255 Step 25
 
Upvote 0
Unless you're "Eagle Eyed", an 8 point increment might be as close as you'll get in one go if you want to prevent the error that Leith mentioned.
I can't see the difference between (RGB(0, 0, 0) and RGB(8, 0, 0) but maybe you can.
If you change Dante's code to the following, you might get away with it. (Something like Yasser's suggestion.)
Code:
  For r = 0 To 255 Step 8
    For g = 0 To 255 Step 8
      For b = 0 To 255 Step 8
 
Upvote 0
If you want to get the RGB and Index for a selected cell, this would do that.
Code:
Sub Show_Colour()
' Thanks to VoG
Dim RGBColour As String, R As Integer, G As Integer, B As Integer
RGBColour = Right("000000" & Hex(ActiveCell.Interior.Color), 6)
R = WorksheetFunction.Hex2Dec(Right(RGBColour, 2))
G = WorksheetFunction.Hex2Dec(Mid(RGBColour, 3, 2))
B = WorksheetFunction.Hex2Dec(Left(RGBColour, 2))
MsgBox "RGB" & vbTab & R & ", " & G & ", " & B & vbCrLf & "Index" & vbTab & ActiveCell.Interior.ColorIndex
End Sub
 
Upvote 0
In fact there are 16 million combinations. 256 * 256 * 256 = 16,777,216
...

Hi,
Good Day,
Thank you for your time to provide a macro for my need, i have run it in my pc, i am using Excel 2010 in Windows 10, i got the desired result, but i got error message in 64,096th row, i dont know why it happened but debug report highlights at
Cells(i, j).Interior.Color = RGB(r, g, b)
Cells(i, j).Value = "RGB(" & r & "," & g & "," & b & ")"
Could you please advise,
and one more request, if it possible can we get the result in column wise? i.e for 1 to 255 (one color set) in A column and second color set from B column like that.

Advance Thanks for your support
Regards
Witson
 
Last edited by a moderator:
Upvote 0
Hello witsonjoyet,

I don't know which version of Excel your are using but you will most likely encounter the error "Too many different cell formats". I am using Excel 2010 and the error occurred at 65,280 cells.

Hi,
I am using Excel 2010 and got error while using the macro
 
Upvote 0
If you want to get the RGB and Index for a selected cell, this would do that.
Code:
Sub Show_Colour()
' Thanks to VoG
Dim RGBColour As String, R As Integer, G As Integer, B As Integer
RGBColour = Right("000000" & Hex(ActiveCell.Interior.Color), 6)
R = WorksheetFunction.Hex2Dec(Right(RGBColour, 2))
G = WorksheetFunction.Hex2Dec(Mid(RGBColour, 3, 2))
B = WorksheetFunction.Hex2Dec(Left(RGBColour, 2))
MsgBox "RGB" & vbTab & R & ", " & G & ", " & B & vbCrLf & "Index" & vbTab & ActiveCell.Interior.ColorIndex
End Sub

Hi,

Thanks for your code, it is useful one, but for me some how i need to show the color variances too.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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