Setting a font color in VBA

calendar_today Asked Dec 18, 2008
thumb_up 7 upvotes
history Updated April 16, 2026

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)

+7 upvotes ranks this answer #101 out of 136 excel-vba solutions on this site .