简体   繁体   中英

Trouble with nested IF statement

I'm looking for help writing an IF statement within excel. If the letter of a specific cell begins with a, b, c, I'd like it to list AB, if it begins with dk, I'd like it to list BB, if lr, list CB and finally if s - z, list DB.

I was able to get it working by only listing one if statement, but i'm having trouble dding in the others.

WORKED:

=IF(AND(RIGHT(A2,1)>="a",RIGHT(A2,1)<="c"),"AB")

CLEARLY THIS IS WRONG:

=IF(AND(RIGHT(A1,1)>="a",RIGHT(A1,1)<="c"),"AB"),
IF(AND(RIGHT(A1,1)>="d",RIGHT(A1,1)<="k"),"BB"),
IF(AND(RIGHT(A1,1)>="l",RIGHT(A1,1)<="r"),"CB"),
IF(AND(RIGHT(A1,1)>="s",RIGHT(A1,1)<="z"),"DB")

I'm sure this is a really simple fix too but I'm not sure how to get around it.

Thanks

How about a different approach?

=IFERROR( LOOKUP( CODE( UPPER(A1) ), {65,68,76,83,91}, {"AB","BB","CB","DB",""}),"")

CODE(A1) will always pick up the first character in the value passed into it (eg LEFT(A1, 1) )

edit: added one more level to pass empty string in case > Z

You are closing off the If statements too soon.

Try

=IF(AND(Left(A1,1)>="a",Left(A1,1)<="c"),"AB",
IF(AND(Left(A1,1)>="d",Left(A1,1)<="k"),"BB",
IF(AND(Left(A1,1)>="l",Left(A1,1)<="r"),"CB",
IF(AND(Left(A1,1)>="s",Left(A1,1)<="z"),"DB", "what now?"))))

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