Setting font color in VBA

I want to set the font color of a cell for a specific RGB value.

If i use

ActiveCell.Color = RGB(255,255,0) 

I get yellow, but if I use a more exotic RGB value, for example:

 ActiveCell.Color = RGB(178, 150, 109) 

I just get gray back.

Why can't I use any RGB value? And do you know workarounds?

Thanks.

+6
vba excel-vba colors excel rgb
source share
4 answers

Excel uses only colors in the color picker. When you set a cell using the RGB value, it selects the one that is in the palette, which is the closest match. You can update the palette with your own colors, and then select your color, and this will work.

This will allow you to 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 allow you to 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 
+7
source share

Quick tip: in the Excel palette, there are two rows of colors that are rarely used and can usually be set to custom values ​​without visible changes on sheets of other nations.

Here's the code to create a reasonable set of "soft tone" colors that are much less offensive than the default values:

Public Sub SetPalePalette (optional wbk like Excel.Workbook) "This routine creates a custom palette of pale tones that you can use for controls, headers and dialogs

' ** THIS CODE IN THE PUBLIC DOMAIN ** ' Nigel Heffernan http://Excellerando.Blogspot.com

'The Excel color palette has two hidden lines that are rarely used: Line 1: colors from 17 to 24 "Line 2: colors from 25 to 32 - used in SetGrayPalette in this book

The 'code to fix the existing screen update setting and, if necessary, “temporarily suspend the update until this procedure is annoying” flickers on the screen ... and restores the screen update when exiting, if necessary.

Dim bScreenUpdating As Boolean

bScreenUpdating = Application.ScreenUpdating

If bScreenUpdating = True then Application .ScreenUpdating = False End If

'If Application.ScreenUpdating <> bScreenUpdating Then' Application.ScreenUpdating = bScreenUpdating 'End If

If wbk means nothing Set wbk = ThisWorkbook End If

With wbk

 .Colors(17) = &HFFFFD0 ' pale cyan .Colors(18) = &HD8FFD8 ' pale green. .Colors(19) = &HD0FFFF ' pale yellow .Colors(20) = &HC8E8FF ' pale orange .Colors(21) = &HDBDBFF ' pale pink .Colors(22) = &HFFE0FF ' pale magenta .Colors(23) = &HFFE8E8 ' lavender .Colors(24) = &HFFF0F0 ' paler lavender 

End with

If Application.ScreenUpdating <> bScreenUpdating Then Application.ScreenUpdating = bScreenUpdating End If

End Sub

Public Sub SetGreyPalette () 'This routine creates a custom greyshades palette that you can use for controls, headers, and dialogs

' ** THIS CODE IN THE PUBLIC DOMAIN ** ' Nigel Heffernan http://Excellerando.Blogspot.com

'The Excel color palette has two hidden lines that are rarely used: “Line 1: colors from 17 to 24” - used in SetPalePalette in this book' Line 2: colors from 25 to 32

'The code to fix the existing screen update settings and, if necessary,' temporarily suspend the update until this procedure is annoying 'flickers on the screen ... do not forget to restore the screen update when exiting!

Dim bScreenUpdating As Boolean

bScreenUpdating = Application.ScreenUpdating

If bScreenUpdating = True then Application .ScreenUpdating = False End If

'If Application.ScreenUpdating <> bScreenUpdating Then' Application.ScreenUpdating = bScreenUpdating 'End If

With this book .Colors (25) = & HF0F0F0 .Colors (26) = & HE8E8E8 .Colors (27) = & HE0E0E0 .Colors (28) = & HD8D8D8 .Colors (29) = & HD0D0D0 .Colors (30) = & HC8C8C8 '& HC0C0C0' Scipped & HC0C0C0 is the usual 25% gray color in the main palette. Colors (31) = & HB8B8B8 'Note that tears become wider: the human eye is more sensitive. Colors (32) = & HA8A8A8' to changes in bright sulfur, so this will be perceived as a linear scale. End with

'In the right column of the Excel palette, the following gray pages are set by default:

'Colors (56) = & H333333' Colors (16) = & H808080 'Colors (48) = & H969696' Colors (15) = & HC0C0C0 'default '25% gray'

'This should be changed to improve the color of the “gap” and make the colors easily distinguishable

With this book .Flowers (56) = & H505050 .Flowers (16) = & H707070 .Flowers (48) = & H989898 '. Colors (15) = & HC0C0C0 End with

If Application.ScreenUpdating <> bScreenUpdating Then Application.ScreenUpdating = bScreenUpdating End If

End Sub

You can write the CaptureColors and ReinstateColors functions for each Open () and BeforeClose () event workbook ... Or even for each sheet, activate and deactivate the event.

I have a code lying somewhere that creates a “thermal” color gradient for three-dimensional diagrams, giving you progress from “cold” blue to “hot” red in thirty-two steps. This is more complicated than you might think: the gradient of colors that will be perceived by the human visual system at “equal intervals” (which works on a logarithmic intensity scale and has non-linear scales for red, green and blue colors as “strong” colors) takes time to build - and you need to use VBA to force MS Chart to use the colors you specified in the order you specified.

+2
source share
 Sub color() bj = CStr(Hex(ActiveCell.Interior.Color)) If Len(bj) < 6 Then Do Until Len(bj) = 6 bj = "0" & bj Loop End If R = CLng("&H" & Right(bj, 2)) bj = Left(bj, Len(bj) - 2) G = CLng("&H" & Right(bj, 2)) bj = Left(bj, Len(bj) - 2) B = CLng("&H" & bj) End Sub 
+1
source share

Thanks for the answers and comments.

This really caused big problems because my client had other plugins installed in Excel that also faked the color palette.

As a result, I replaced several colors in the palette, and then assigned my elements a specific ColorIndex, but boy, this is not very.

0
source share

All Articles