[英]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()
. 您不需要
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: 如果第一个条件没有自动通过,则数字大于4,因此您只需检查它是否小于9,依此类推...或在公式中:
=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. 您忘了在当时条件之前在右侧关闭
AND
。
The third IF
ended up having a huge condition and no then or else statement, this happened with the last AND
too. 第三个
IF
的状态非常恶劣,并且没有then或else语句,最后一个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")))))
Glad you got it working, but there's another approach you might want to consider rather than nested IFs in a situation like this. 很高兴您能正常工作,但在这种情况下,您可能还想考虑另一种方法,而不是嵌套IF。
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 在其他工作表上或工作表中的其他地方创建一个表格,其中包含您的级别和达到该级别所需的分数:0级别1 4级别2 8级别3 12级别4 15级别5
Then use a VLOOKUP or an INDEX(MATCH function to find matches. I like the INDEX(MATCH since it gives you more control. 然后使用VLOOKUP或INDEX(MATCH函数)查找匹配项。我喜欢INDEX(MATCH,因为它可以为您提供更多控制权。
=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. 在这种情况下,我们的列表将在Sheet1上,级别在B列中列出,而所需的分数在A列中,而我们的公式在工作表2上引用从A1开始的实际得分。
The INDEX
function looks at an array - B1:B5 - and returns a value from a specified row in that array. INDEX
函数查看一个数组-B1:B5-并从该数组中的指定行返回一个值。 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. MATCH
函数查看一个数组-A1:A5-并返回小于或等于匹配值的最大数字在数组中的位置。 It passes this result back to the INDEX
so it can select the appropriate row. 它将结果传回
INDEX
以便可以选择适当的行。
This method is easier to maintain, modify and expand than a multi-level nested IF
. 与多层嵌套
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. 分数是输入单元格的定义名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.