英文:
IF and ELSE in excel
问题
> 输入图像描述
值1 值2 颜色
2 16 =IF(A2>5,"红色", IF(AND(5>A2>1,40>B2>1,"黄色")),IF(AND(5>A2>1,40>B2>60,"红色")),IF(AND(5>A2>1,60>B2>100,"黄色")),"绿色")
我认为其逻辑如下:
如果 "值1" > 5 ---> "红色"
如果 "值1" 在1-5之间且 "值2" 在1-40之间 ---> "黄色"
如果 "值1" 在1-5之间且 "值2" 在40-60之间 ---> "红色"
如果 "值1" 在1-5之间且 "值2" 在60-100之间 ---> "黄色"
否则 ---> "绿色"
我希望根据两个不同列中的两个值显示颜色 "红色"、"黄色" 或 "绿色"。我尝试查找相关信息,但找不到可以使我的公式起作用的东西。希望图片能够解释清楚。
它只显示公式错误,但我无法弄清楚该怎么做...
英文:
> enter image description here
Value1 Value2 Color
2 16 =IF(A2>5,"Red", IF(AND(5>A2>1,40>B2>1,"Yellow")),IF(AND(5>A2>1,40>B2>60,"Red")),IF(AND(5>A2>1,60>B2>100,"Yellow")),"Green)
The logic for which, I believe is:
If "Value1" > 5 ---> "Red"
If "Value1" is between 1-5 and "Value2 " is between 1-40 ---> "Yellow"
If "Value1" is between 1-5 and "Value2 " is between 40-60 ---> "Red"
If "Value1" is between 1-5 and "Value2 " is between 60-100 ---> "Yellow"
Else ---> "Green"
I want the color «red», «yellow» og «green» to be shown in a column based on two values from two different colums. I’ve tried to look it up but I can’t find anything to make my formula work. Hope the picture explains
It only says that the formula is wrong but I can’t figure out what to do…
答案1
得分: 1
=IF(A2>5, "红色", IF(AND(A2>1, B2>1, B2<40), "黄色", IF(AND(A2>1, B2>40, B2<60), "红色", IF(AND(A2>1, B2>60, B2<100), "黄色", "绿色")))
或者使用这个
=IF(OR(B2>=100, A2<=1), "绿色", IF(OR(A2>5, 60>B2>40), "红色", "黄色"))
英文:
=IF(A2>5, "Red", IF(AND(A2>1, B2>1, B2<40), "Yellow", IF(AND(A2>1, B2>40, B2<60), "Red", IF(AND(A2>1, B2>60, B2<100), "Yellow", "Green"))))
or use this
=IF(OR(B2>=100, A2<=1), "Green", IF(OR(A2>5, 60>B2>40), "Red","Yellow"))
答案2
得分: 0
=IF(AND(A2<=5,B2<100,B2>60),"Yellow",IF(OR(A2>5,AND(B2>=40,B2<60)),"Red","Green"))
(根据您的请求,我只提供代码的翻译部分)
英文:
Try this:
=IF(AND(A2<=5,B2<100,B2>60),"Yellow",IF(OR(A2>5,AND(B2>=40,B2<60)),"Red","Green"))
(Edited as I hadn't realised the mid-range for Value2 in 40-60 was to go red)
Rather than use (to me anyway) the confusing 1>x>100
way of expressing a range, I've instead put range boundaries as separate conditions within an AND
clause. It's no better or worse, but I find it easier to parse that way.
答案3
得分: 0
感谢反馈!很抱歉以错误的格式发布问题,下次会做得更好。
在 Excel 中对我有效的公式是:(我现在从手机上写这个,所以对于任何代码块错误和类似问题,敬请谅解)
=IF(A2>5, "红色", IF(AND(1<A2,A2<5,1<B2,B2<40), "黄色", IF(AND(1<A2,A2<5,40<B2,B2<60), "红色", IF(AND(1<A2,A2<5,60<B2,B2<100), "黄色", "绿色"))))
英文:
thanks for the feedback! And sorry for posting the question in a wrong «format», will do better next time.
The formula that worked for me in excel is: (i am writing this from my phone so sorry for any code block errors and such)
=IF(A2>5; «Red»; IF(AND(1<A2;A2<5;1<B2;B2<40); «Yellow»; IF(AND(1<A2;A2<5;40<B2;B2<60); «Red»; IF(AND(1<A2;A2<5;60<B2;B2<100); «Yellow»; «Green»))))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论