Excel:如果所有条件都为真,则嵌套的IF语句返回所有真值

时间:2016-04-20 17:20:25

标签: excel if-statement concatenation nested-if

我想为所有执行的语句返回多个值true。我使用以下公式:

=IF(
AND(I4=1,J4=1),"2G",
IF(
AND(Q4=1,V4=1),"3G",
IF(
AND(AC4=1,AG4=1),"4G",
"Other")))

然而,这是从嵌套的IF返回第一个真值。我想要归还所有三个值,2G 3G 4G,如果一切都是真的。类似于concatenated的结果。

2 个答案:

答案 0 :(得分:4)

没有得到“2G3G4G”,如果没有,那么就得到“其他”,我建议像这样:

=TRIM(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE("     "," ",IF(AND(I4=1,J4=1)," 2G"," "),1)," ",IF(AND(Q4=1,V4=1),"3G "," "),3)," ",IF(AND(AC4=1,AG4=1),"4G"," "),5),"     ","other"))

如果您有任何疑问,请询问;)

修改

对于在回答时正在改变的问题,我将提供一些“通用”代码。

如果您想要排列所有解决方案,可以使用:

=SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(IF([C_1],[E_1]&" ","  ")&IF([C_2],[E_2]&" ","  ")&...&IF([C_n],[E_n],"  "),REPT("  ",[TC]),[NS]))," ",[ND]),[SR]," ")
  • [C_1][C_2],...,[C_n]:每个不同表达的条件
  • [E_1][E_2],...,[E_n]:表达本身
  • [TC]:此公式中所有IF的总数
  • [NS]:如果没有解决方案,则表达
  • [ND]:您想要的新分隔符
  • [SR]:使用过的“空间替代品”

注意:对于此解决方案,您不能使用包含空格的表达式。只需用你不使用的符号替换它们,如下划线。 ("Exp. 1"之类的表达式将为"Exp._1")这将通过[SR]更改回来。

所以你的解决方案看起来像这样:

=SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(IF(OR(I4=1,J4=1),"2G ","  ")&IF(AND(Q4=1,V4=1),"3G ","  ")&IF(AND(AC4=1,AG4=1),"4G","  "),REPT("  ",3),"other"))," ","/"),[SR]," ")

因为表达式不包含空格,所以可以跳过[SR] - 部分:

=SUBSTITUTE(TRIM(SUBSTITUTE(IF(OR(I4=1,J4=1),"2G ","  ")&IF(AND(Q4=1,V4=1),"3G ","  ")&IF(AND(AC4=1,AG4=1),"4G","  "),REPT("  ",3),"other"))," ","/")

如果您还有任何疑问,请询问。

答案 1 :(得分:3)

您有一个If()公式,因此只要找到第一个TRUE,它就会停止。相反,只需将它们连接起来:

=If(And(I4=1,J4=1),"2G","") & if(and(q4=1,v4=1),"3G","") & if(and(ac4=1,ag4=1),"4G","")

编辑:刚刚意识到这可能会让你留下一个说“" 2G3G4G"”的单元格,我认为这并不是你想要的。如果需要,只需添加逗号分隔符即可。

edit2:根据您的评论,您可能需要这样:

=If(Or(I4=1,J4=1),"2G","") & if(Or(q4=1,v4=1),"3G","") & if(Or(ac4=1,ag4=1),"4G","")

这将检查I4=J1 J4=1。如果其中任何一个是1,它将返回" 2G"。与Q4V4相同,如果其中一个是1,则会返回" 3G"。