Why are font color values negative?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,687
Office Version
  1. 365
Platform
  1. Windows
While working on a macro to do some highlighting, I had the need to change both the fill color and the font color. Using the macro recorder, I learned that I can set the fill color to red (255,0,0) using this code:

Code:
With Selection.Interior
  .Pattern = xlSolid
  .PatternColorIndex = xlAutomatic
  .color = 256
  .TintAndShade = 0
  .PatternTintAndShade = 0
End With

I then learned that I can set the font color to red using this code:

Code:
With Selection.Font
  .color = -16776961
  .TintAndShade = 0
End With

It turns out that -16776961 = 256^3 - the RGBcolor value.

Why is the font color not just the RGB value, like the fill color is?
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
256 shouldn't give you red, that number appears visually to be black. But if you want to use RGB representations of numbers just use the RGB function.

.Color=RGB(255,0,0)

That value also isn't 256^3. That is equal to 16777216

Here's a link I found out about it: https://social.msdn.microsoft.com/Forums/office/en-US/df8a1e1e-e974-4a9c-938a-da18ae9f5252/vba-excel-2010-rgb-to-long-color-value-convertor?forum=exceldev

but I would just use the RGB function personally.

There are also eight named color constants which you can also use:

vbBlack
vbRed
vbGreen
vbYellow
vbBlue
vbMagenta
vbCyan
vbWhite

Code:
Selection.Font.Color=vbRed
 
Last edited:
Upvote 0
Color property takes a value supplied by RGB function. Range of values returned by RGB function is 0 to 16777215.

When you provide -16776961 as a value for Color property it exceeds the acceptable value range. Therefore the value wraps around and the actual value assigned to the Color property is 16777215 + (-16776961) + 1 = 255.
If you read the value of that object again with Selection.Font.Color, it will return 255.

All .Color properties take RGB values as input : Border, Borders, Font, Interior, Tab.
https://msdn.microsoft.com/en-us/vba/excel-vba/articles/interior-color-property-excel
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,677
Messages
6,173,793
Members
452,534
Latest member
autodiscreet

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