Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Excel Color Index - handy tool

0

I had an application that required me to use different colours for text, lines etc (Trend Chart with various trend pens )... long story short, I had to learn Excel Colours, sometimes by Index Number, sometimes by RGB() values, and a couple times by HEX values... So I created my own HELP workbooks to allow me to get a handle on colours.

Excel has 56 Index Colours (numbered 1 to 56, with black being Index #1 )

On the worksheet in column B, I put the numbers 1 through 56 and ran a subroutine which coloured the appropiate cells in column A

Option Explicit
Sub ColorCells()
    Dim rng As Range
    Set rng = Range("B1") ' Change the range to the desired cells
    Dim i As Long

    rng.Select
    ' Loop through each ColorIndex value and apply it to the range
    For i = 0 To 56
        ActiveCell.Offset(1, -1).Value = i
        ActiveCell.Offset(1, 0).Select
        With Selection.Interior
            .ColorIndex = i
        End With

    Next i
End Sub

I then created a function in vba to read the interior colour index as RGB value

Function getRGB(RefCell)
Dim mystr As String
Application.Volatile
    mystr = Right("000000" & Hex(RefCell.Interior.Color), 6)
    getRGB = Application.Hex2Dec(Right(mystr, 2)) & ", " & _
             Application.Hex2Dec(Mid(mystr, 3, 2)) & ", " & _
             Application.Hex2Dec(Left(mystr, 2))
End Function

So in a separate column, I used this function on each appropriate cell to call back the RGB values for each of the 56 colour indexes.

I could have simply redone the same thing to capture the HEX values... but I like to make things tricky.... LOL

Excel already has a Decimal to Hexidecimal Function (DEC2HEX), but you have to do a little work to convert RGB values to Hexidecimal format...

in my case, the RGB values were in Column D... so in the appropriate cells in column E, I put in the following formula.. and dragged it down the column

="#"&DEC2HEX(MID(D2,2,FIND(",",D2,1)-2),2)&DEC2HEX(MID(D2,FIND(",",D2,1)+1,FIND(",",D2,FIND(",",D2,1)+1)-FIND(",",D2,1)-1),2)&DEC2HEX(MID(D2,FIND(",",D2,FIND(",",D2,1)+1)+1,FIND(")",D2,1)-FIND(",",D2,FIND(",",D2,1)+1)-1),2)

(you have to parse out the individual characters, convert them to two digits when necessary, and then concatenate them back together)

For the Names (of each color), I went to ChatGPT and asked to translate RGB values to their proper name .. here is what was provided

Index Colour Name 1 Black 2 White 3 Red 4 Green 5 Blue 6 Yellow 7 Magenta 8 Cyan 9 Dark Red 10 Dark Green 11 Dark Blue 12 Olive 13 Purple 14 Teal 15 Silver 16 Grey 17 Lavender 18 Plum 19 Cream 20 Pale Aqua 21 Dark Purple 22 Light Salmon 23 Medium Blue 24 Very Light Blue 25 Navy Blue 26 Magenta 27 Yellow 28 Cyan 29 Dark Purple 30 Dark Maroon 31 Dark Teal 32 Blue 33 Bright Cyan 34 Pale Cyan 35 Pale Green 36 Pale Yellow 37 Sky Blue 38 Pink 39 Soft Lavender 40 Peach 41 Bright Blue 42 Aqua 43 Lime Green 44 Golden Yellow 45 Orange 46 Deep Orange 47 Steel Blue 48 Medium Grey 49 Dark Blue-Black 50 Sage Green 51 Deep Forest Green 52 Olive Drab 53 Dark Rust 54 Deep Rose 55 Indigo 56 Dark Grey

I wasn't sure if you would accept macro-enabled files.... so I just copied the code & formulas... but that should give you enough to create your own colour help workbook

Answer
Discuss

0

Hello Mykal and welcome to the forum,

Nice work. Having a quick reference like that is quite handy. I made one for myself but not quite as elaborate as yours. Mine has colummns for Index #, Colour Name, the colour, Same As (ex: index colour 5 is the same as index colour 32), and lastly the RGB code.

As for posting files, yes, "xlsm" files are allowed. At the bottom of the page where you posted the info, to the left of the "Submit Post" button is another button "Add Files to the Post". Clicking this button allows you to add up to 3 files to your post.

When posting code in your post it is preferred to use the "CODE" button. Clicking this will place " [ CODE ] Code _ Goes _ Here [ / CODE ] " in your post. Remove "Code_Goes_Here" and paste in your code.

Example:

    rng.Select
    ' Loop through each ColorIndex value and apply it to the range
    For i = 0 To 56
        ActiveCell.Offset(1, -1).Value = i
        ActiveCell.Offset(1, 0).Select
        With Selection.Interior
            .ColorIndex = i
        End With
    Next i

This makes reading and copying code easier. 

Cheers   :-)

Discuss


Answer the Question

You must create an account to use the forum. Create an Account or Login