简体   繁体   中英

Excel: How do i construct nested If condition in Excel formula

This is an grading formula based on the score.

If score < 4 Then LEVEL 1
If score > 4 and score < 9 LEVEL 2
If score > 8 and score < 13 LEVEL 3
If score > 12 and score < 15 LEVEL 4
If score > 15 LEVEL 5

I really could not figure out, Where is the mistake. Please help.

=IF(A1<4,"LEVEL 1",IF(AND(A1>4,A1<9),"LEVEL 2",IF(AND(A1>8,A1<13,"LEVEL 3",IF(AND(A1>12,A1<17,"LEVEL 4",IF(A1>16,"LEVEL 5")))))))

You don't need AND() . If 1st condition doesn't pass automatically number is greater than 4 thus you just check if it is lower that 9 and so on... Or in a formula:

=IF(A1<4,"LEVEL 1",IF(A1<9,"LEVEL 2",IF(A1<13,"LEVEL 3",IF(A1<17, "LEVEL 4", "LEVEL 5"))))

您几乎已经掌握了它,您只需要确保关闭AND语句的括号即可:

=IF(A1<4,"LEVEL 1",IF(AND(A1>4,A1<9),"LEVEL 2",IF(AND(A1>8,A1<13),"LEVEL 3",IF(AND(A1>12,A1<17),"LEVEL 4",IF(A1>16,"LEVEL 5")))))

You forgot to close your AND 's in the right before the then conditions.
The third IF ended up having a huge condition and no then or else statement, this happened with the last AND too.

=IF(A1<4,              "LEVEL 1",
 IF(AND(A1>4, A1<9),   "LEVEL 2",
 IF(AND(A1>8, A1<13),  "LEVEL 3",
 IF(AND(A1>12, A1<17), "LEVEL 4",
 IF(A1>16,             "LEVEL 5")))))

Glad you got it working, but there's another approach you might want to consider rather than nested IFs in a situation like this.

Create a table on another sheet or elsewhere on your sheet with your levels and the score needed to reach them: 0 Level 1 4 Level 2 8 Level 3 12 Level 4 15 Level 5

Then use a VLOOKUP or an INDEX(MATCH function to find matches. I like the INDEX(MATCH since it gives you more control.

=INDEX(Sheet1!$B$1:$B$5,MATCH(A1,Sheet1!$A$1:$A$5,1))

In this case, our list would be on Sheet1, with the Levels listed in column B and the required scores in column A, while our formula is on sheet 2 referencing actual scores that start in A1.

The INDEX function looks at an array - B1:B5 - and returns a value from a specified row in that array. The MATCH function looks at an array - A1:A5 - and returns the location in the array of the highest number that is less than or equal to the match value. It passes this result back to the INDEX so it can select the appropriate row.

This method is easier to maintain, modify and expand than a multi-level nested IF .

You could also use this formula:

=CHOOSE(MATCH(Score,{0,4,8,12,15},1),"Level 1","Level 2","Level 3","Level 4","Level 5")

Score is a defined name to the cell of your input.

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