缺少逗号和空格

时间:2017-04-13 03:45:30

标签: excel

我一直在检查这个公式,预计结果是 Apple,Orange,Grape **。但得到了结果** AppleOrange,Grape,。缺少逗号和空格以及额外的逗号

enter image description here

enter image description here

=IF(A2="","",$A$1)
&IF(D2="","",$D$1)
&IF(OR(A2="",D2=""),"",", ")
&IF(G2="","",$G$1)
&IF(OR(A2="",D2="",G2=""),"",", ")
&IF(J2="","",$J$1)
&IF(OR(A2="",D2="",G2="",J2=""),"",", ")
&IF(M2="","",$M$1)
&IF(OR(A2="",D2="",G2="",J2="",M2=""),"",", ")
&IF(O2="","",$O$1)
&IF(OR(A2="",D2="",G2="",J2="",M2="",O2=""),"",", ")
&IF(Q2="","",$Q$1)
&IF(OR(A2="",D2="",G2="",J2="",M2="",O2="",Q2=""),"",", ")
&IF(S2="","",$S$1)
&IF(OR(A2="",D2="",G2="",J2="",M2="",O2="",Q2="",S2=""),"",", ")
&IF(U2="","",$U$1)
&IF(OR(A2="",D2="",G2="",J2="",M2="",O2="",Q2="",S2="",U2=""),"",", ")
&IF(W2="","",$W$1)
&IF(OR(A2="",D2="",G2="",J2="",M2="",O2="",Q2="",S2="",U2="",W2=""),"",", ")
&IF(X2="","",$X$1)
&IF(OR(A2="",D2="",G2="",J2="",M2="",O2="",Q2="",S2="",U2="",W2="",X2),"",", ")
&IF(Y2="","",$Y$1)
&IF(OR(A2="",D2="",G2="",J2="",M2="",O2="",Q2="",S2="",U2="",W2="",X2="",Y2),"",", ")

文件 https://www.dropbox.com/s/4q4fv34m0tbcp3h/stack.xlsx?dl=0

3 个答案:

答案 0 :(得分:1)

这是一种缩短的方法。遵循这种模式:

=IF(A2="","",A1&", ")&IF(D2="","",D1&", ")&IF(G2="","",G1&", ")

然后用它包装它以除去尾随的逗号:

=IF(RIGHT(stuff,1)=",",LEFT(stuff,LEN(stuff,-1),stuff))

此外,对于疯狂的长公式(但仅在绝对必要时)我强烈建议使用excel beautifier

答案 1 :(得分:1)

我认为你的逻辑略有偏差。您的逻辑应该是,从第二个水果专栏Orange开始,如果先前出现任何水果,您有条件地在水果名称之前添加一个逗号

考虑以下覆盖Apple和Orange的公式部分:

=IF(A2="yes", $A$1, "")
&IF(D2="yes", IF(A2="yes", ", ", "")&$D$1, "")

如果存在橙色,我们会打印一个可选的前面的逗号,但是只有 ,如果它出现在它之前,在这种情况下意味着Apple存在。然后,我们打印" Orange"在这个可选的逗号之后。对于其余列重复该逻辑。所有真正改变的是OR表达式,它检查是否存在一个水果,这是保证添加逗号分隔符所需的全部内容。

=IF(A2="yes", $A$1, "")
&IF(D2="yes", IF(A2="yes", ", ", "")&$D$1, "")
&IF(G2="yes", IF(OR(A2="yes", D2="yes"), ", ", "")&$G$1, "")
&IF(J2="yes", IF(OR(A2="yes", D2="yes", G2="yes"), ", ", "")&$J$1, "")
&IF(M2="yes", IF(OR(A2="yes", D2="yes", G2="yes", J2="yes"), ", ", "")&$M$1, "")
&IF(O2="yes", IF(OR(A2="yes", D2="yes", G2="yes", J2="yes", M2="yes"), ", ", "")&$O$1, "")
&IF(Q2="yes", IF(OR(A2="yes", D2="yes", G2="yes", J2="yes", M2="yes",
                    O2="yes"), ", ", "")&$Q$1, "")
&IF(S2="yes", IF(OR(A2="yes", D2="yes", G2="yes", J2="yes", M2="yes",
                    O2="yes", Q2="yes"), ", ", "")&$S$1, "")
&IF(U2="yes", IF(OR(A2="yes", D2="yes", G2="yes", J2="yes", M2="yes",
                    O2="yes", Q2="yes", S2="yes"), ", ", "")&$U$1, "")
&IF(W2="yes", IF(OR(A2="yes", D2="yes", G2="yes", J2="yes", M2="yes",
                    O2="yes", Q2="yes", S2="yes", U2="yes"), ", ", "")&$W$1, "")
&IF(X2="yes", IF(OR(A2="yes", D2="yes", G2="yes", J2="yes", M2="yes",
                    O2="yes", Q2="yes", S2="yes", U2="yes", W2="yes"), ", ", "")&$X$1, "")
&IF(Y2="yes", IF(OR(A2="yes", D2="yes", G2="yes", J2="yes", M2="yes",
                    O2="yes", Q2="yes", S2="yes", U2="yes", W2="yes",
                    X2="yes"), ", ", "")&$Y$1, "")

答案 2 :(得分:1)

编辑 - 改为使用此公式。类似,但你不需要额外的行:

=IF(COUNTIFS(B3:Z3,TRUE)<1,"",LEFT(CONCAT(IF(B3:Z3,$B$2:$Z$2&", ","")),LEN(CONCAT(IF(B3:Z3,$B$2:$Z$2&", ","")))-2))

这比if语句更好,因为你可以在不改变公式的情况下轻松地添加和减去水果。

原始答案:

将您的“是”更改为“真”。在上方添加一行,在左侧添加一列。

在上面的行中,添加您的姓名,逗号和空格。例如。 “Apple,”

在左栏中使用公式:

=IF(COUNTIFS(B3:Z3,TRUE)<1,"",LEFT(CONCAT(IF(B3:Z3,$B$1:$Z$1,"")),LEN(CONCAT(IF(B3:Z3,$B$1:$Z$1,"")))-2))
编辑公式时,

CTRL + Shift + Enter将使其成为数组公式,这是您工作所需的公式

这是做什么的: 1)如果没有真蛋,则使用空格 2)在你拥有TRUE的地方添加所有标题 3)删除最后一个逗号和空格。

见图。 enter image description here

如果您确实希望单个水果显示为空白,请使用&lt; = 1而不是&lt; 1