Conditional Formatting

Location: http://www.mvps.org/dmcritchie/excel/condfmt.htm      
Home page: http://www.mvps.org/dmcritchie/excel/excel.htm
[View without Frames]

Conditional Formatting was introduced with Excel 97 and is a terrific feature, but there is a limit of 3 conditional sets per cell grouping (like 3 wishes).  Conditional Formatting, while in effect for a cell, will override the text colors that can be produced for numeric values by normal cell formatting

The limit of 3 does not apply to the sheet but to the grouping of cells.  You cannot include a cell already in one conditional cell grouping in another. (refined definition 2001-03-27)

You can identify which cells have Conditional Formatting by using Edit, Goto, Conditional Formatting.  You can see what the Conditional Formatting is for a specific cell or a group by selecting the cell(s) and Format, Conditional Formatting, which will show the already existing Conditional Formatting.

The idea behind Conditional Formatting and filters for that matter is to test for a condition, which will provide either TRUE or FALSE.
  =1=1 will produce TRUE because 1=1
  =1=2 will produce FALSE because 1<>2  (not equal to)

Whatever is not covered with Conditional Formatting will be covered by normal cell formatting so if you want the default to be black italic you can format the column as such with normal cell formatting.

The format painter will both normal cell formatting and Conditional Formatting to additional cells.  The format painter is an optional button on the toolbar that looks like a paint brush.

Coloring date in a column based on today's date -- TODAY()

 

Select Column B
Format --> Conditional Formatting
    [add format] for condition 2
    [add format] for condition 3

condition 1    Formula is:
=AND(B1<TODAY(),B1>TODAY()-8)
format: font is red and italic

condition 2    Formula is:
=AND(B11>TODAY(),B1<TODAY()+8)
format font is blue and italic

condition 3    Formula is:
=B1=TODAY()
format font is black and bold

   
 ABC
1 Date Testing  
2 03/16/2001  
3 03/17/2001  
4 03/18/2001  
5 03/19/2001  
6  03/20/2001 
7  03/21/2001 
8  03/22/2001 
9  03/23/2001 
10  03/24/2001 
11  03/25/2001 
12  03/26/2001 
13 03/27/2001  
14  03/28/2001 
15  03/29/2001 
   
 ABC
1 Date Testing
16  03/30/2001 
17  03/31/2001 
18  04/01/2001 
19  04/02/2001 
20  04/03/2001 
21 04/04/2001  
22 04/05/2001  
23 04/06/2001  
24 04/07/2001  

Color Row based on value in a Column

Select all cells (Ctrl+a)
menu: Format, Conditional Formatting, Formula is on drop down box
Condition 1:  =$D1<=1.00   [interior Color 35 Light Green]
Condition 2:  =$D1<=1.05   [interior Color 36 Light Yellow]
Condition 3:  =$D1>1.05     [interior Color 38 Rose]

Since the conditions are checked in order it is frequently unnecessary to have more than one comparison in a condition.  For example in Condition 2 above, we do not need to write the condition as
  =AND($D1>1.00,=$D1<-1.05)

You might for instance change the first condition to =AND($D1<=1.00,$D1>0)   so that normal cell formatting will be seen for zero and negative numbers.

 
 ABCDE
1description Col(B)Col(C) =B/C=GetFormula(Dn)
2description 1 33 200 0.165 =IF(ISERROR(B2/C2),"",B2/C2)
3description 2 500 475 1.053 =IF(ISERROR(B3/C3),"",B3/C3)
4description 3 105 100 1.050 =IF(ISERROR(B4/C4),"",B4/C4)
5description 4 -5 4 (1.250) =IF(ISERROR(B5/C5),"",B5/C5)
6description 5 560 400 1.400 =IF(ISERROR(B6/C6),"",B6/C6)
7description 6 75 160 0.469 =IF(ISERROR(B7/C7),"",B7/C7)
8description 7 0 1 0.000 =IF(ISERROR(B8/C8),"",B8/C8)
9description 8     =IF(ISERROR(B9/C9),"",B9/C9)
10description 9   -1 0.000 =IF(ISERROR(B10/C10),"",B10/C10)

In order to not color the vast unused rows and cells without numbers the actual Conditional Formatting used in the above example is as follows:
Condition 1:  =AND(TRIM($D1)<>"",$D1<=1.00)
Condition 2:  =AND(TRIM($D1)<>"",$D1<=1.05)
Condition 3:  =AND(TRIM($D1)<>"",$D1>1.05,ISNUMBER($D1))

hint:  When filling in the formulas press F2 so that you can use backspace to correct mistakes, also a good idea when working on cells in a spreadsheet.

While setting up each condition before completion of that condition, you change the text color, interior color pattern, borders as desired.  Since interior color will wipe out gridlines, you might also want to include borders.

When a condition is removed, it is like it never existed. Conditional Formatting therefore will not leave garbage such as colored cells laying around once removed.  You may have a problem finding your Conditional Formatting though.

Multiple formatting

Select all cells (Ctrl+a)
menu: Format, Conditional Formatting, Formula is on drop down box
Condition 1:  =COUNTIF(A1:I1,"completed")>3   [interior Color 35 Light Green]
Condition 2:  value equal to: completed   [interior Color 36 Light Yellow]
 ABCDEFGHI
1Sample 5Test 1 3Test 2 10Test 3 3Test 4
210145 Completed2 2 of 310 Completed2 2 of 3
31015 5 Completed 3 Completed 10 Completed 3 Completed
410164 4 of 5  of 3  of 10  of 3
510175 Completed3 Completed9 9 of 103 Completed
61018 5 Completed 3 Completed 10 Completed 3 Completed
710191 1 of 5  of 3  of 10  of 3
81020   of 5   of 3   of 10   of 3
910215 Completed3 Completed10 Completed2 2 of 3
101022   of 5   of 3   of 10   of 3
11  nnn nnnnnnnnnnnnn nnn nnnnnnnnnnnnn nnn nnnnnnnnnnnnn nnn nnnnnnnnnnnnn
12C2:  =IF(B2>= B$1,"Completed",B2 & " of " & B$1)
If you wanted to be more specific with the C.F. you could to something like:

Select ALL cells for condition 1:
    format --> Conditional Formatting -->
    formula1 is: =SUM($c1="completed",$e1="completed",$g1="completed",$i1="completed")>=4

Select columns: C, E, G, I for condition 2:
    format --> Conditional Formatting -->
        Formula2 is: ="completed"

Color Banding

The following is an example of AutoFilter which is described in more detail on my sumdata.htm page.  The conditional formatting formula shown includes SUBTOTAL so that it applies only to the visible cells whether filtering is involved or not.  With banding there is no problem with also formatting the rows below the used area.  
Conditional Format:   =MOD(SUBTOTAL(3,$C1:D$2),3)=0
 A B C
1Prod.  Color  Price 
2AB0010Black 13.50
3AB0011White 13.75
4AB0012 Yellow 14.00
5AB0013Green 14.25
6AB0021Black 14.50
7AB0022 White 14.75
8AB0023Yellow 15.00
9AB0024Green 15.25
10AB0035 Black 15.50
11AB0036White 15.75
12AC0010Black 16.00
13AC0011 White 16.25
   
 A B C
1 Prod.  Color  Price 
2AB0010 Black 13.50
6AB0021 Black 14.50
10 AB0035Black 15.50
12AC0010 Black 16.00
16AC0021 Black 14.00
20 AC0035Black 15.00
24AD0010 Black 15.50
28AD0021 Black 13.75
32 AD0035Black 14.75

 
The above table is a representation of the results of Data, Filter, Auto Filter.  For more information on auto filtering in this example see my sumdata.htm page.

Additional information on Color Banding can be found in the Related Area at the bottom of this page.

More Notes on Conditional Formatting

The key to using Conditional Formatting (C.F.) is to reduce a formula so that it indicates TRUE or FALSE (1 or 0).  C.F. overrides normal cell formatting.  You are limited to the use of three conditional formatting sets per worksheet (like three wishes).  C.F. began with XL97.  For XL95 you would have to write your own Event macros.

 
Here is a nice summary presented by Leo Heuser 01-07-28, misc.

Please note the use of mixed absolute ($$) and relative (no dollars) references.

  1. Select column B (click the B in the column bar)
  2. Choose Formats > Conditional formatting
  3. With "Condition 1" choose "Formula is"
  4. Enter this formula:  =COUNTIF($B$1:B1,B1)>1
  5. Click the button "Formats" and choose a proper format. OK.OK
  6. In another column, C1, enter this formula:
         =IF(COUNTIF($B$1:B1,B1)>1,"Number exists in row " & MATCH(B1,$B$1:B1,0);"")
  7. Drag C1 down with the fill handle (the little square in the lower right
    corner of the cell)
  
    BCD
1Name Leo's markingPeo's marking
2Allen  
3Jack  
4Casey  
5Jack ... exists in row 3... exists in cell B3
6David  
7Casey ... exists in row 4 ... exists in cell B4 
8Jerome   
conditional format:  =COUNTIF($B$1:B1,B1)>1
 
Chip Pearson has specific information on
Checking for Duplicates
alternate formula: Peo Sjoblom in the same thread.
=IF(COUNTIF($B$1:B1,B1)>1,"Number exists in cell" &ADDRESS(MATCH(B1,$B$1:B1,0),COLUMN(B1),4),"")

Some specific examples, as they appear in newsgroups:

These examples tell what to select before invoking Format --> Conditional Formatting and all are Formula is, and after coding your formula, choose your format (pattern and font color), the first you see without selecting a tab is the FONT.  When choosing an interior color you might also choose the borders because gridlines disappear with interior color and the borders chosen will only exist while C.F. is in effect.

To get smallest half (rounded up) of the entries in bold, change the formatting formula to
    Formula is:  =A1<=SMALL($A1:$T1,(COUNTA(A$1:T$1)+1)/2)

Cells with Formulas:  You will need a User Defined Function for this and your UDF must be in the same workbook or referenced in your VBE (tools, reference). 
You cannot use =personal.xls!HasFormula(A1)  for instance. 
    Formula is:  =HasFormula(A1)

Function HasFormula(cell)
  HasFormula = cell.HasFormula  'in same  workbook  for C.Formatting
End Function
Alternative:  You can temporarily see which cells have formulas with Ctrl+A, Edit, GoTo, [Special], Formulas, (check all: Numbers, Text, Logicals, Errors)

Cells with Formulas -- without using a User Defined Function:  Ian Brown (2001-11-10)  Here's a way to do it without using VBA!  Select cell A1 on any sheet in the workbook, Define the name CellHasFormula (using Insert | Name | Define) as
    =GET.CELL(48,!A1)   -- In Format | Conditional Formatting use a "Formula Is" setting of
    =CellHasFormula       -- to trigger the defined formats.

Consecutive Duplicates:  To compare value in column A with value below, but not include the difference being blank or empty.  Select ALL cells then enter the following conditional formatting formula: 
=AND($A1<>"",OR($A1=$A2,OFFSET($A1,-1,0)=$A1))

Duplicated Anywhere in Column:  Need not be sorted
    Formula is:  =COUNTIF($A:$A,$A1)>1
    which, of course, is similar to using a cell in another column to convey the same information: =IF(COUNTIF($A$1:$A1,$A1)=1,"","Duplicate")

Highlight the top 4 numbers in a range -- can result in missing numers, if 4th is tied with 5th numbers if the the 4th highest number is tied
=A1>LARGE($B$2:$B$200,5)

Highlight the top 4 numbers in a range -- can result in duplicate numbers, if 4th is tied with 5th
=A1>=LARGE($B$2:$B$200,4)

Highlight the maximum in a specific column
=$C1=MAX($C:$C)

Highlight the maximum in each of specific columns -- preselect columns i.e. C:H
--no solution yet--

Finding each of several numbers in a table , Debra Dalgleish.  The example involved 100 people named in A2:A101 each with six numbers in columns B:H.  The numbers to be checked for are entered in $J2:$J$100.  To enter the Conditional Format, first select B2:H101 then for C.F. use formula is: =ISNUMBER(MATCH(B2,$J$2:$J$100,0)) and select a color on the Pattern tab.  Be sure to use absolute references for the arguments in J2:J100 in the C.F.

Finding Conditional Formatting Formulas afterwards

Conditional Formatting Formulas are considerably harder to show and find once used, unless you know which range to pick.  Each Conditional Format has a range that you can't just see, anyway the following is a start and will work best if it just has a formula, rather than "is less than" type of conditions.

Function CondFormula(myCell, Optional cond As Long = 1) As String
  'Bernie Deitrick programming 2000-02-18, modified D.McR 2001-08-07, 2002-02-07
  Application.Volatile
  CondFormula = ""
  On Error Resume Next
  CondFormula = myCell.FormatConditions(cond).Formula1
End Function
Without On Error you will get #Value! if there is no conditional formatting.

Example:
Column A set up for conditional formatting
Formua is: $A1 > 3

G1:   =CondFormula(a1,1)
H1:   =CondFormula(a1,2)
I1:   =CondFormula(a1,3)

And then this still isn't much of a start if you want to know if the Conditional Formatting actually applies and want to know what resulting coloring or other formatting actually was applied.

If you used A1>3 you would see G1>3 in your G1 cell, so there are still some quirks in this.


Preparation of examples on this page

The examples on this page were created with the help of macros found on my xl2html.htm page.  The macros do not recognize anything done by Conditional Formatting so they have to be adjusted manually before conversion.  This is a nice feature of C.F. because it really does not affect anything, no values or formulas are changed.  Currently by choice XL2HTML() macro does not automatically right align numbers.  Macros were changed recently (2000-11-27) to include 4 special fonts.  Cell formatting colors have to be corrected manually.  The purpose of these macros is to keep the output small and usable.

Conditional Formatting / Normal Formatting / Event macros

If you only needed two (possibly 3) font colors for numeric values + one color for text values you may be able to use normal cell formatting.  Normal cell formatting does not support interior (background) coloring.

If you have more than 3 sets of conditions then you probably want to use an EVENT macro.

Hopefully this provided sufficient information to get you started.  It is a bit tricky at first.  Note the mixed reference $D1 meaning that on row 1 only the $H column will be checked.  This example is assuming that your active cell is on row 1 when you start.

If you do not want to color the entire row but only columns B, G:I for instance then only select those columns before you start doing your Conditional Formatting.  It will be harder to find any of your 3 Conditional Formatting though (warning).  See function later on this page.

It is easier to work with Conditional Formatting if you always work form all cells selected if you can work with having an entire row selected. However there are functions and subroutines to help you find them see my [sumdata.htm page. -- This will be included here.]

Normal Cell Formatting

My Formulas page Show FORMULA of another cell in Excel has additional examples of normal cell formatting under the GetFormat() macro.  These are some of those shown.

 

  VALUE =GetFormat(A...) =GetFormula(A...) +
  A B C D
11 Yes [Red][>0]"No";[Green]"Yes" =-1 F
12 No [Red][>0]"No";[Green]"Yes" =1 F
13 5.00 [Blue][>=5]0.00;[Red][<-2]-0.00;[Yellow]
General;[magenta]"Text:"@
=5 F

Here is an example using the "Webdings 2" font. 

 
 ABCDEF
2wanted  value=BFormat 
3green/circle 1 Ä [Color50][=1]"Ä";[Color46][=2]"ã";[Red]"¢";[Color13]"³"
4orange/asterisk  2 ã [Color50][=1]"Ä";[Color46][=2]"ã";[Red]"¢";[Color13]"³"
5red/block 3 ¢ [Color50][=1]"Ä";[Color46][=2]"ã";[Red]"¢";[Color13]"³"
6any text entry A ³ [Color50][=1]"Ä";[Color46][=2]"ã";[Red]"¢";[Color13]"³"
7 
8wingdings 2X == code: Alt+nnnn from numeric keypad
9  Ä Ä Ä 0196Ä
10  ³ ³ ³ 0179³
11  ¢ ¢ ¢ 0162¢
12  ã ã ã 0227ã
13  Ì Ì Ì 0204Ì
14  Ó Ó Ó 0211Ó
15  G G G 0071G
16  H H H 0072H
17  I I I 0073I
18Color5046 Red  
19       
20Colors http://www.mvps.org/dmcritchie/excel/colors.htm
21symbols http://www.mvps.org/dmcritchie/rexx/htm/symbols.htm

The following table and additional information can be found on my Colors page.

(Additional keywords: color, colour, coloring, colouring)
1 53 52 51 49 55 56
9 46 12 10 5 47 16
3 45 43 50 41 13 48
7 44 6 4 33 54 15
38 40 36 35 37 39 2
  Hex equivalents used in HTML
#000000#993300#333300#003300 #003366#333399#333333
#800000#FF6600#808000 #008000 #0000FF #666699 #808080
#FF0000 #FF9900 #99cc00 #339966 #3366FF #800080 #969696
#FF00FF #FFcc00 #FFFF00 #00FF00 #00ccFF #993366 #c0c0c0
#FF99cc #FFcc99 #FFFF99 #ccFFcc #99ccFF #cc99FF #FFFFFF
Excel only recognizes Color 1 through 8 by name
(Black, White, Red, Green, Blue, Yellow, Magenta, and Cyan).

Related Areas

The Related Area here was copied from my Summarizing Data, and Auto Filter (an Overview) Related Areas since only a few items do not relate to Conditional formatting those items have also been retained.

Older articles archived on former Deja are now available back to Mar 29, 1995.  Between May 2000 and mid April 2001 things had been bad with articles before 19May1999 missing since before 9July2000, see my Newsgroups page.


This page was introduced on November 27, 2000. 

[My Excel Pages -- home]    [INDEX to my site and the off-site pages I reference] 
[Site Search -- Excel]     [Go Back]    [Return to TOP

Please send your comments concerning this web page to: David McRitchie mailto:DMcRitchie@msn.com