如何创建一个列出包含“是”的前一个字段的标题的字段?

时间:2016-07-26 18:38:37

标签: excel excel-vba excel-formula vba

我有以下格式的Excel表格:

╔════════╦════════╦════════╦════════╦═════════╗
║ Field1 ║ Field2 ║ Field3 ║  ...   ║ Field10 ║
╠════════╬════════╬════════╬════════╬═════════╣
║   no   ║  no    ║  no    ║  ...   ║  no     ║
║   no   ║  yes   ║  no    ║  ...   ║  no     ║
║   yes  ║  yes   ║  yes   ║  ...   ║  yes    ║
║   yes  ║  yes   ║  no    ║  ...   ║  yes    ║
║   .    ║  .     ║  .     ║  ...   ║  .      ║
║   .    ║  .     ║  .     ║  ...   ║  .      ║
║   .    ║  .     ║  .     ║  ...   ║  .      ║
╚════════╩════════╩════════╩════════╩═════════╝

如您所见,每个字段可以包含yes和no的任意组合

我正在尝试创建一个基于之前显示的数据格式的单个字段。该字段将包含包含“是”的字段名称。如果多个字段中存在“是”,则应列出以逗号分隔的字段。

以下是所需字段的示例:

╔════════╦════════╦════════╦════════╦═════════╦══════════════════════════╗
║ Field1 ║ Field2 ║ Field3 ║  ...   ║ Field10 ║         NewField         ║
╠════════╬════════╬════════╬════════╬═════════╬══════════════════════════╣
║   no   ║  no    ║  no    ║  ...   ║  no     ║                          ║
║   no   ║  yes   ║  no    ║  ...   ║  no     ║ Field2                   ║
║   yes  ║  yes   ║  yes   ║  ...   ║  yes    ║ Field1, ..., Field10     ║
║   yes  ║  yes   ║  no    ║  ...   ║  yes    ║ Field1, Field2, Field10  ║
║   .    ║  .     ║  .     ║  ...   ║  .      ║ ...                      ║
║   .    ║  .     ║  .     ║  ...   ║  .      ║ ...                      ║
║   .    ║  .     ║  .     ║  ...   ║  .      ║ ...                      ║
╚════════╩════════╩════════╩════════╩═════════╩══════════════════════════╝

我试图用excel公式实现这一点,但到目前为止,对我来说唯一明显的解决方案包括在excel公式中包含所有可能的排列。当然,创建和更改它是低效且耗时的。有没有办法有效地实现这一结果?

3 个答案:

答案 0 :(得分:4)

如果您没有Office 365 Excel,那么这是一个自定义UDF,可以执行您想要的操作:

Function JoinField(ttl As Range, srchrng As Range, crit As Variant, Optional sep As String = ",") As String
Dim ttlArr() As Variant
Dim srchrngArr() As Variant
Dim i&,j&
ttlArr = ttl.Value
srchrngArr = srchrng.Value

If UBound(ttlArr, 1) <> UBound(srchrngArr, 1) Or UBound(ttlArr, 2) <> UBound(srchrngArr, 2) Then Exit Function

For i = LBound(ttlArr, 1) To UBound(ttlArr, 1)
    For j = LBound(ttlArr, 2) To UBound(ttlArr, 2)
        If srchrngArr(i, j) = crit Then
            JoinField = JoinField & ttlArr(i, j) & sep
        End If
    Next j
Next i

JoinField = Left(JoinField, Len(JoinField) - Len(sep))


End Function

将其放在工作簿附带的模块中。 请勿将其放在工作表代码或ThisWorkbook代码中。

然后将其称为普通函数:

=JoinField($A$1:$J$1,$A2:$J2,"Yes",",")

第一个标准是要连接的名称。第二个是具有标准的Range。第三个是要找的标准。第四个是可选的分离字符。默认值为,

enter image description here

如果您拥有最新的Office 365 Excel,则可以将此公式用作数组。

=TEXTJOIN(",",TRUE,IF($A2:$J2 = "Yes", $A$1:$J$1,""))

作为一个数组,当退出编辑模式而不是Enter时,需要使用Ctrl-Shift-Enter确认。如果操作正确,Excel会将{}放在公式周围。

答案 1 :(得分:1)

为了证明FORMULA使用多个IF公式会有多繁琐和冗长,你最终可能看到如下公式:

=IF(LEFT(IF(A2="yes",A$1,"")&IF(B2="yes",", "&B$1,"")&IF(C2="yes",", "&C$1,"")&IF(D2="yes",", "&D$1,"")&IF(E2="yes",", "&E$1,"")&IF(F2="yes",", "&F$1,"")&IF(G2="yes",", "&G$1,"")&IF(H2="yes",", "&H$1,"")&IF(I2="yes",", "&I$1,"")&IF(J2="yes",", "&J$1,""),1)=",",SUBSTITUTE(IF(A2="yes",A$1,"")&IF(B2="yes",", "&B$1,"")&IF(C2="yes",", "&C$1,"")&IF(D2="yes",", "&D$1,"")&IF(E2="yes",", "&E$1,"")&IF(F2="yes",", "&F$1,"")&IF(G2="yes",", "&G$1,"")&IF(H2="yes",", "&H$1,"")&IF(I2="yes",", "&I$1,"")&IF(J2="yes",", "&J$1,""),", ","",1),IF(A2="yes",A$1,"")&IF(B2="yes",", "&B$1,"")&IF(C2="yes",", "&C$1,"")&IF(D2="yes",", "&D$1,"")&IF(E2="yes",", "&E$1,"")&IF(F2="yes",", "&F$1,"")&IF(G2="yes",", "&G$1,"")&IF(H2="yes",", "&H$1,"")&IF(I2="yes",", "&I$1,"")&IF(J2="yes",", "&J$1,""))

上面的公式将第1行假定为您的字段标题,并将第一个字段设为起始列A.上述公式将放在K2中并向下复制。

现在,如果由于某种原因你不允许使用VBA或以.XLSM格式保存你的工作表,那么你需要上面那个可怕的公式。

没有滚动条,公式看起来更像:

=IF(LEFT(IF(A2="yes",A$1,"")&IF(B2="yes",", "&B$1,"")&IF(C2="yes",", "&C$1,"")&
IF(D2="yes",", "&D$1,"")&IF(E2="yes",", "&E$1,"")&IF(F2="yes",", "&F$1,"")&
IF(G2="yes",", "&G$1,"")&IF(H2="yes",", "&H$1,"")&IF(I2="yes",", "&I$1,"")&
IF(J2="yes",", "&J$1,""),1)=",",
SUBSTITUTE(IF(A2="yes",A$1,"")&IF(B2="yes",", "&B$1,"")&IF(C2="yes",", "&C$1,"")&
IF(D2="yes",", "&D$1,"")&IF(E2="yes",", "&E$1,"")&IF(F2="yes",", "&F$1,"")&
IF(G2="yes",", "&G$1,"")&IF(H2="yes",", "&H$1,"")&IF(I2="yes",", "&I$1,"")&
IF(J2="yes",", "&J$1,""),", ","",1),
IF(A2="yes",A$1,"")&IF(B2="yes",", "&B$1,"")&IF(C2="yes",", "&C$1,"")&
IF(D2="yes",", "&D$1,"")&IF(E2="yes",", "&E$1,"")&IF(F2="yes",", "&F$1,"")&
IF(G2="yes",", "&G$1,"")&IF(H2="yes",", "&H$1,"")&IF(I2="yes",", "&I$1,"")&
IF(J2="yes",", "&J$1,""))

工作中的丑陋证据

POC

一个主要的缺点是,如果你需要添加另一个字段,编辑公式将是一个皇家痛苦的屁股!

答案 2 :(得分:0)

如果只有10列,你可以选择......像这样

= IF(A2 = 1;间接(&#34; A1&#34 ;; TRUE);&#34;&#34;)&amp; &#34;,&#34; &安培; IF(B2 = 1;间接(&#34; B1&#34 ;; TRUE);&#34;&#34;)&amp; &#34;,&#34; &安培; ....

在此示例中,只添加了0,1作为值而不是是/否。