简体   繁体   中英

Excel conditional formatting row if cell starts with

I'm trying to format a row based off a cell within it.

My table runs from A6 to K207 and I want to highlight the row in bold if the cell in B6 starts with SA.

I tested my formula

=IF(LEFT(TRIM(B26),2)="SA","YES","NO") 

in another cell and that works ok - but I can't get the formatting to work for some reason...

I removed the yes/no and placed into a conditional formatting rule applying to my whole table and it doesn't work. Even if I apply it to just a row it doesn't work.

Am I using if statements within conditional formatting right? I've looked on Google and here and have found similar issues but not the same one, they've all used set values/ranges but not a 'starts with' condition.

The conditional formatting is an IF statement by default, and formats depending on whether the formula is TRUE or FALSE therefore an additional IF statement is not needed:

=LEFT($A6,2)="SA"

This will do the trick, apply this to the entire range as needed.

It will test whether all cells in column a start with "SA" and highlight that cell. If it is applied to a range including more columns, the $ sign fixes the column as A so tests that cell and highlights every cell based on that, effectively highlighting the entire row.

You can also use an OR statement to pass multiple arguments like so:

=OR(LEFT($A6,2)="SA",LEFT($A6,2)="BO")

Welcome to SO. When doing conditional formating (CF) in a formula, you need to apply references.

Switch between relative, absolute, and mixed references

Right now your formula is =IF(LEFT(TRIM(B26),2)="SA","YES","NO") . You are using relative references, so the CF rule will check according to their position. In A6 it will check the value of A6, and I guess you would like the rule to check the value always in column B.

So the first fix you need in yor formula will be (it needs more, hold on)

=IF(LEFT(TRIM($B26),2)="SA","YES","NO") 

Now, it will check always the value in column B. But we need to fix something else. To make the CF based on a formula, you need to formulate something that returns a boolean value, True or False . Your values Yes or NO are strings (text), not boolean, so they don't work.

So try this formula and let's see if it works:

=IF(LEFT(TRIM($B26),2)="SA",TRUE,FALSE) 

An IF Functions allows you to make a question where the answer must be yes/no (true/false) and, depending of the answer, Excel will do an option or another.

With CF rule based on a formula, you just need the question itself, you don't need to choose an option. So the short version of this formula, as @Plutian states in his answer, is just doing:

=LEFT(TRIM($B26),2)="SA"

Try to adapt this to your needs.

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