简体   繁体   中英

How to conditional format a range based on the values of 3 columns

First of all, I'm sorry for my lack of terms and poor knowledge on the subject. I tried to gather code fragments but no luck. The problem is as follows and I'd very much appreciate it if you could give me some pointers.

I have 3 columns (AB and C) with some values. Then, i have 2 columns D and E which i need to conditional format based on AB and C values. The conditions are the following:

D-> If D1 is between A1+B1 and A1+C1 Then D1 stays in black color Else D1 appears in red color

E-> If E1 is between B1 and C1 then E1 stays in black color Else E1 appears in red color

The process then repeats for D2, D3, etc. Same goes for E.

There will be different sheets with the same code and i probably will be adding several rows to some of them, which might tamper with the definition of the range in AB and C. Also, for each day, I'll be adding two sets of columns (ex: first day I'll add D and E, then F and G, etc). Can i double loop the code on D column for odd numbers? and for even numbers regarding E column?

These are my main questions.

  • Select column D (entire column)
  • Add a new conditional formatting rule with the formula below and format it red

     =NOT(AND($D1>$A1,$D1<$B1,$D1<$C1)) 
  • Select column E (entire column)

  • Add a new conditional formatting rule with the formula below and format it red

     =NOT(AND($E1>$B1,$E1<$C1)) 

Note that "between" means the borders (in column A, B, C) are excluded if you want to include them you need to chang > to >= and < to <= .

在此处输入图片说明

You can also add a conditional formatting with VBA:

With ThisWorkbook.Worksheets("Sheet1").Columns("D").FormatConditions.Add(Type:=xlExpression, Formula1:="=NOT(AND($D1>$A1,$D1<$B1,$D1<$C1))")
    .Font.Color = -16776961
    .Font.TintAndShade = 0
End With

But note that the formula in Formula1 must be localized. That means if you plan to run this on a non-english Excel then this formula must be converted to this localized non-english formula.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM