When does Excel use white font on tabs? - excel

Excel uses black by default for the Font Color on the tabs. When you change the tab color, the Font Color remains black if the tab color is light, but changes to white when a darker color is chosen.
I would like to mimic this behavior. For which RGB combinations does Excel change to white Font Color?
Many thanks!

The possible colors of the tab are 256^3. Try it out for yourself:
Public Sub TestMe()
Dim cnt As Long
For cnt = 1 To 256 ^ 3 Step 10
ActiveSheet.Tab.Color = cnt
Next cnt
End Sub

I have followed the approach suggested by #Vityata. Excel behavior is not straight forward to replicate, but I have found an approximation that serve my purposes:
Considering the color cube as in:
http://matlab.izmiran.ru/help/toolbox/images/colorcube.jpg
Excel approximately uses white Font color when
R * 20132 + G * 64005 + B * 6630 <= 11675430
This will fail in some edge cases (eg: RGB(255,102,3)), but I still hope someone finds this useful. I am leaving the question open in case anyone founds an exact formula.
Thanks for the replies.

Related

How can I set a color when using Range.BorderAround?

I am trying to automate the creation of a data table. I found a border color that I want to have for the created table. .Color = 15849925 and\or .ColorIndex = 24.
But when I do MyRange.BorderAround xlContinuous, xlThin, 24 the color that appears on the sheet is not the same. It's purplish instead of pale blue. BorderAround also accepts Color instead of ColorIndex, but when I try MyRange.BorderAround xlContinuous, xlThin, , 15849925 it comes out black (no color).
I also tried using the Color argument while ColorIndex was set to xlColorIndexAutomatic or xlColorIndexNone. No difference, it still appears black.
The obvious workaround would be to set the borders without color and then color them each one by one afterward. But I am really wondering what I've done wrong with BorderAround. How can I set the color when using Range.BorderAround?
Excel 2007 12.0.4518.1014

wordcloud2 drops long words, which gets the colors mismatched

so i have a dynamic list of text that i want to visualize in a wordcloud. sometimes some of the words are too long and they get dropped from the display. i can scale down the size of everything, but it's not always clear what scale i should get down to in order to prevent things from being dropped. i'd rather not fiddle with this and just accept some things are dropped. also, everything is in a shiny app and i have a slider to control scale if really needed. the problem though is i want my text colored by properties of the words in my dataset. in this example below you can see how each "word" has a color associated with it...
wc <- data.frame(
word = c("too big to fit this one is","red","green","blue"),
freq = c(2,1,2,3),
col = c("black","red","green","blue"),
stringsAsFactors = FALSE
)
>wc
word freq col
1 too big to fit this one is 2 black
2 red 1 red
3 green 2 green
4 blue 3 blue
wordcloud2(wc, color = wc$col)
this then draws the wordcloud but the first element is dropped and the colors don't drop too ("red" is colored black, "green" is colored red, and "blue" is colored green). i can't do wordcloud2(wc, color = col) like an aes style call in ggplot, the wordcloud does draw but all the text is clear... i can hover over it but not see any of it. anyone else work through this issue? thanks!!!

IF functions in excel - use multiple ones with the same outcome (explained)?

Here's my dilemma:
I have columns of colours (red, blue, etc). For each there's either 0 or 1 (1 if the item has blue in it, 0 if not). BUT there can be 2 (or 10) colours in an item. Is there a way to formulate this in Excel?
I tried using IF, but this is limiting me to 2 conditions: if the red is false, then it'd go to blue, but I cannot seem to find a simultaneous way of showing both blue and red for an item.
For example:
My skirt is both green and yellow. I have 0 for red, 0 for blue, 1 for green, 1 for yellow. I'd like this condensed in 1 column only, that would say: green, yellow (with a comma in between).
Help?
Guessing what is where, ie that red is in B1, the following seems to work for the example provided:
=LEFT(IF(B2=1,B$1&", ","")&IF(C2=1,C$1&", ","")&IF(D2=1,D$1&", ","")&IF(E2=1,E$1&", ",""),LEN(IF(B2=1,B$1&", ","")&IF(C2=1,C$1&", ","")&IF(D2=1,D$1&", ","")&IF(E2=1,E$1&", ",""))-2)
(Returns error where all colour flags are 0). Most of that is stripping off the last two characters so might be better to use a helper column and two formulae (but much shorter overall).
This part:
=IF(B2=1,B$1&", ","")&IF(C2=1,C$1&", ","")&IF(D2=1,D$1&", ","")&IF(E2=1,E$1&", ","")
may be extended with further IF statements to cover more than four colours.
Were the latter version in R2 then the last two characters (comma space) may be stripped with:
=LEFT(R2,LEN(R2)-2)
A VBA version (copy/paste to a regular code module in your workbook)
Function Colors(names, flags)
Dim rv, i
rv = ""
For i = 1 To names.Cells.Count
If flags(i) = 1 Then rv = rv & _
IIf(Len(rv) > 0, ", ", "") & names(i)
Next i
Colors = rv
End Function
Usage:
=Colors($B$1:$J$1,B2:J2)
Assuming your color names are in B1:J1 and first row of 1/0 is in B2:J2

FormatConditions and Interior Color

I have an old macro that was working fine on Excel 2003 but creates issues with Excel 2010. The part that causes problems is:
If Not IsNull(someRange.FormatConditions(parActiveCondition).Interior.Color) Then
locVisibleColor = someRange.FormatConditions(parActiveCondition).Interior.Color
End if
where parActiveCondition is the active conditional formatting number on someRange.
When the background is selected as "No Color", someRange.FormatConditions(parActiveCondition).Interior.Color returns
Null in Excel 2003
0 in Excel 2010
The problem is that a black background also returns 0. So in Excel 2010 it seems no longer possible to make the difference between a black background and no background color.
Does anybody know a workaround?
ps: I could obviously select a white background instead of "No Color" but I'd rather not change all the spreadsheets and conditional formatting rules.
You could use a Boolean secondary check such as
IsNull(someRange.FormatConditions(parActiveCondition).Interior.ColorIndex) 'or
IsNull(someRange.FormatConditions(parActiveCondition).Interior.TintAndShade)
when
.FormatConditions(parActiveCondition).Interior.Color = 0
Option Explicit
Sub test()
Dim Color
Dim R As Integer
Dim G As Integer
Dim B As Integer
Color = ThisWorkbook.Sheets(1).Range("A1").Interior.Color
R = Color Mod 256
G = (Color \ 256) Mod 256
B = (Color \ 256 \ 256) Mod 256
ThisWorkbook.Sheets(1).Range("B1").Interior.Color = RGB(R, G, B)
End Sub
Black returns (0,0,0)
It appears however that "no color" returns (255,255,255) (=white)

Given a fixed cel width, can I change the fontsize so that the text will fit inside the cell?

I have code that will automatically adjust the height of a cell, so that text will fit inside that cell.
However now I want to change the fontsize of the text inside the cell, so that the font will shrink and grow to make the text fit inside the cell, so I don't have to make the cell width a fixed size.
How do I do this in VBA?
I need to know the width of text, given a font size
Option 1.
TextWidth = WidthOfAText(Range("A1").Value)
FontAdjustmentFactor = Range("A1").EntireColumn.ColumnWidth / TextWidth
FontSize = Range("A1").Font.Size * FontAdjustmentFactor
Range("A1").Font.Size = Max(4, Min(10, FontSize))
or I need to know whether the text will fix and do some sort of trial and error routine.
Option 2.
Range("A1").Font.Size = 10
While (Text_does_not_fit AND Range("A1").Font.Size >= 5)
Range("A1").Font.Size = Range("A1").Font.Size - 1
Wend
How do I do this? (Preferably using code like in option 1)
Non programatically just right click on the cell -> Format Cells -> Alignment -> Shrink to Fit
Code wise is:
Sheet1.Range("A1").ShrinkToFit = True

Resources