change font colour if cell contains function - programmatically

  • Thread starter Thread starter fitful_thought
  • Start date Start date
F

fitful_thought

How do I get the font color to be black if cells(3,3) contains "=today()"
but to be blue if cells(3,3) contains (for example) "30 Oct 2004"?
 
Here's the code way:

Sub test()
With Cells(3, 3)
.Interior.Color = IIf(.HasFormula, vbBlue, vbBlack)
End With
End Sub


There is also a Named Range method, which is kind of dodgy and crashes XL97
users under specific conditions - on my website "IsFormula"
 
Hi fitful_thought,

fitful_thought said:
How do I get the font color to be black if cells(3,3) contains
"=today()" but to be blue if cells(3,3) contains (for example) "30
Oct 2004"?

Something like this may do what you need:

Sub ColorCells()
On Error Resume Next
With Sheet1.UsedRange
.SpecialCells(xlCellTypeFormulas).Font.Color = vbBlack
.SpecialCells(xlCellTypeConstants).Font.Color = vbBlue
End With
On Error GoTo 0
End Sub

This changes font colors for the entire usedrange of a worksheet - you could
use the HasFormula property of the Range object to determine if the cell
contains a formula or not:

Sub ColorCells2()
With Sheet1.Range("A3")
If .HasFormula Then
.Font.Color = vbBlack
Else
.Font.Color = vbBlue
End If
End With
End Sub

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]
 
Thanks Rob,
That solution works well.
I've never used Iif before.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top