Excel, get conditinal formatting font color in VBA
I have a spreadsheet in Excel that uses the conditional formatting formulas to set cell font colors when they exceed certain limits.
e.g. cells > x have their font color set to Blue.
I've been trying to read the individual cell font colors from within a VBA function, e.g.
Code:
Debug.Print rng.Font.Color
Debug.Print rng.Font.FontStyle
Debug.Print rng.Font.ColorIndex
Debug.Print rng.Interior.Color
But this doesn't seem to know about the conditional formatting changes,
i.e. it returns the basic, default color info for the cell.
Is the actual, current font color info, as seen by the user, discernable via code somewhere?
Thanks, DaveBo
Re: Excel, get conditinal formatting font color in VBA
The first thing you want to do, if you look for the coding of something in VBA, use the macro recorder to do it manually and look at the code created by this recording.
I created a conditional format and the recorder gave me this:
VB Code:
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
Formula1:="35"
With Selection.FormatConditions(1).Font
.Bold = True
.Italic = False
End With
I hope you can take it from here
Re: Excel, get conditinal formatting font color in VBA
Opus, Thanks, that's good info.
It tells me about the formulas used, but it still doesn't tell me if the formulas have actually been tripped and any conditions set.
i.e. it isn't telling me the current color of the cell.
Thanks, DaveBo
Re: Excel, get conditinal formatting font color in VBA
That's correct, however you can check using VBA if the condition is met, and you can check all possible formattings. It seems like a long way but it's possible.
Re: Excel, get conditinal formatting font color in VBA
Quote:
Originally Posted by DaveBo
Is the actual, current font color info, as seen by the user, discernable via code somewhere?
Thanks, DaveBo
I'm sure there is, but you could also check to see if the condition is met.
e.g. If rgn > x then msgbox "This cell color has been changed"
Implying that if the condition is true, the cell certainly has changed color.
Just a workaround.
p.s. I think that's what the previous post meant. Sorry..
Re: Excel, get conditinal formatting font color in VBA
I tried actually reading and parsing the format condition formulas to recreate the calculations it would make and determine what it should have done,
however ...
the conditional formatting was set on a large range of cells, with relative addresses used, i.e. the formula for the target cell compared the cell contents in the same column as the target cell but at row 1 and the contents in the same row but at columns 3 & 4.
Works fine in the sheet, but when reading the formulas in VBA the relative cell addresses were inconsistent, sometimes they pointed to the the proper columns & rows, other times they were the original formulas without the addresses adjusted for "relativity".
Anyway, I gave up on that approach.