Question posted 2008 · +6 upvotes
I want to set the font color of a cell to a specific RGB value.
If I use
ActiveCell.Color = RGB(255,255,0)
I do get yellow, but if I use a more exotic RGB value like:
ActiveCell.Color = RGB(178, 150, 109)
I just get a grey color back.
How come can’t I just use any RGB value? And do you know any workarounds?
Thanks.
Accepted answer +7 upvotes
Excel only uses the colors in the color palette. When you set a cell using the RGB value, it chooses the one in the palette that is the closest match. You can update the palette with your colors and then choose your color and that will work.
This will let you see what is currently in the palette:
Public Sub checkPalette()
Dim i As Integer, iRed As Integer, iGreen As Integer, iBlue As Integer
Dim lcolor As Long
For i = 1 To 56
lcolor = ActiveWorkbook.Colors(i)
iRed = lcolor Mod &H100 'get red component
lcolor = lcolor &H100 'divide
iGreen = lcolor Mod &H100 'get green component
lcolor = lcolor &H100 'divide
iBlue = lcolor Mod &H100 'get blue component
Debug.Print "Palette " & i & ": R=" & iRed & " B=" & iBlue & " G=" & iGreen
Next i
End Sub
This will let you set the palette
Public Sub setPalette(palIdx As Integer, r As Integer, g As Integer, b As Integer)
ActiveWorkbook.Colors(palIdx) = RGB(r, g, b)
End Sub
2 code variants in this answer
- Variant 1 — 13 lines, starts with
Public Sub checkPalette() - Variant 2 — 3 lines, starts with
Public Sub setPalette(palIdx As Integer, r As Integer, g As…
Top excel-vba Q&A (6)
- How to clear the entire array? +58 (2010)
- How to change Format of a Cell to Text using VBA +55 (2011)
- Download attachment from Outlook and Open in Excel +43 (2012)
- Can a VBA function in Excel return a range? +36 (2009)
- 2 Dimensional array from range +34 (2013)
- Hiding an Excel worksheet with VBA +33 (2009)
excel-vba solutions on this site
.