ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   IF Statement and Font Color (https://www.excelbanter.com/excel-programming/383048-if-statement-font-color.html)

[email protected]

IF Statement and Font Color
 
Is there a way to have an IF statement check a cell and do something
according to the color of the font?


merjet

IF Statement and Font Color
 
Yes, with VBA (not IF worksheet function). An example:

Sub macro1()
If Sheets("Sheet1").Range("A1").Font.ColorIndex = 1 Then
Sheets("Sheet1").Range("B1") = "black"
ElseIf Sheets("Sheet1").Range("A1").Font.ColorIndex = 3 Then
Sheets("Sheet1").Range("B1") = "red"
ElseIf Sheets("Sheet1").Range("A1").Font.ColorIndex = 10 Then
Sheets("Sheet1").Range("B1") = "green"
End If

Hth,
Merjet



Tom Ogilvy

IF Statement and Font Color
 
in xl2003 and earlier, the interior color of a cell can be one of 56 values
or xlnone.

if activecell.Interior.ColorIndex = 3 then
' the cells interior is red

to see the Colors associated with the colorindex

Sub ShowColors()
for i = 1 to 56
cells(i,1).Interior.ColorIndex = i
cells(i,2).Value = i
Next
End Sub

Note that the user can create a custom palatte in a workbook and map
different colors to the 56 available colorindexes.

A good discussion of Excel and colors is at
http://www.mvps.org/dmcritchie/excel/colors.htm

--
Regards,
Tom Ogilvy


" wrote:

Is there a way to have an IF statement check a cell and do something
according to the color of the font?



Chip Pearson

IF Statement and Font Color
 
Try something like the following:


If Range("A1").Font.ColorIndex = 3 Then ' red
MsgBox "Red Cell"
End If



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)

wrote in message
oups.com...
Is there a way to have an IF statement check a cell and do something
according to the color of the font?




Tom Ogilvy

IF Statement and Font Color
 
missed the "Font" part

If activecell.Font.ColorIndex = 3 then
' font color is red

Instead of xlNone for the interior of the cell, the setting is xlautomatic
(if you don't want to assign a color, but let excel set it based on the
color scheme).

--
Regards,
Tom Ogilvy




"Tom Ogilvy" wrote in message
...
in xl2003 and earlier, the interior color of a cell can be one of 56
values
or xlnone.

if activecell.Interior.ColorIndex = 3 then
' the cells interior is red

to see the Colors associated with the colorindex

Sub ShowColors()
for i = 1 to 56
cells(i,1).Interior.ColorIndex = i
cells(i,2).Value = i
Next
End Sub

Note that the user can create a custom palatte in a workbook and map
different colors to the 56 available colorindexes.

A good discussion of Excel and colors is at
http://www.mvps.org/dmcritchie/excel/colors.htm

--
Regards,
Tom Ogilvy


" wrote:

Is there a way to have an IF statement check a cell and do something
according to the color of the font?






All times are GMT +1. The time now is 12:38 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com