简体   繁体   English

Excel公式:抓取与特定单元格相对应的列名称

[英]Excel formula : Grab the column name corresponding to particular cell

Excel表格

In Above excel sheet i need a formula which will extract that particular column name where "1" is present and enter that name in the corresponding cell. 在上面的excel工作表中,我需要一个公式,该公式将提取出出现“ 1”的特定列名称,并在相应的单元格中输入该名称。 Example- In above Image each row corresponding to col "Tags" contain the column name whose corresponding cell contain "1". 示例-在上图中,与col“标签”相对应的每一行都包含其相应单元格包含“ 1”的列名。

For a 4 column example you can use this: 对于4列示例,您可以使用以下代码:

=SUBSTITUTE(TRIM(IF(A2,$A$1,"")&" "&IF(B2,$B$1,"")&" "&IF(C2,$C$1,"")&" "&IF(D2,$D$1,""))," ",", ")

For 7 columns, just add additional IF statements inside the TRIM following the same pattern. 对于7列,只需按照相同的模式在TRIM内添加其他IF语句即可。

The formula relies on 1=TRUE to keep it short. 该公式依靠1=TRUE来使其简短。 The result each IF is followed by a space. 每个IF的结果后跟一个空格。 The TRIM gets rid of extra spaces left when no 1 occurs. 当没有1发生时, TRIM会消除剩余的多余空间。 Finally SUBSTITUTE converts 最终SUBSTITUTE转换 into , so you get a comma delimited list. 到中,因此您会得到一个逗号分隔的列表。

Note that: 注意:

TRIM strips extra spaces from text, leaving only single spaces between words and no space characters at the start or end of the text. TRIM会从文本中删除多余的空格,单词之间仅留一个空格,而在文本的开头或结尾则不留空格。

在此处输入图片说明

couldn't find a non vba solution for concatenating a range. 找不到用于连接范围的非vba解决方案。 So here is a UDF 这是UDF

Function conCatRange(ByVal criteriaRange As range, _
                        ByVal criteria As String, _
                        ByVal conRange As range, _
                        ByVal separator As String) As String

    Dim c As range
    conCatRange = ""

    For i = 1 To conRange.Columns.Count
        If (criteriaRange(1, i) = criteria) Then
            conCatRange = conCatRange & conRange(1, i) & separator
        End If
    Next i

    conCatRange = Left(conCatRange, Len(conCatRange) - 1)
End Function
  1. criteriaRange = Range you want check for 1s, standardRange =您要检查1s的范围,
  2. criteria = 1 (in your case) 条件= 1(在您的情况下)
  3. conRange = header range conRange =标头范围
  4. separator = "," 分隔符=“,”

if you want to add summary and parameter descriptions see this link How to put a tooltip on a user-defined function 如果要添加摘要和参数说明,请参见此链接。 如何在用户定义的函数上放置工具提示

Supposing your header range is AK, 假设标题范围是AK,

You could you use this formula from L2 onwards: 您可以从L2开始使用此公式:

=IF(MID(TRIM(IF(A2=1,A$1,"")&IF(B2=1,", "&B$1,"")&IF(C2=1,", "&C$1,"")&IF(D2=1,", "&D$1,"")&IF(E2=1,", "&E$1,"")&IF(F2=1,", "&F$1,"")&IF(G2=1,", "&G$1,"")&IF(H2=1,", "&H$1,"")&IF(I2=1,", "&I$1,"")&IF(J2=1,", "&J$1,"")&IF(K2=1,", "&K$1,"")),1,2)=", ",MID(TRIM(IF(A2=1,A$1,"")&IF(B2=1,", "&B$1,"")&IF(C2=1,", "&$C$1,"")&IF(D2=1,", "&D$1,"")&IF(E2=1,", "&E$1,"")&IF(F2=1,", "&F$1,"")&IF(G2=1,", "&G$1,"")&IF(H2=1,", "&H$1,"")&IF(I2=1,", "&I$1,"")&IF(J2=1,", "&J$1,"")&IF(K2=1,", "&K$1,"")),3,1000),TRIM(IF(A2=1,A$1,"")&IF(B2=1,", "&B$1,"")&IF(C2=1,", "&C$1,"")&IF(D2=1,", "&D$1,"")&IF(E2=1,", "&E$1,"")&IF(F2=1,", "&F$1,"")&IF(G2=1,", "&G$1,"")&IF(H2=1,", "&H$1,"")&IF(I2=1,", "&I$1,"")&IF(J2=1,", "&J$1,"")&IF(K2=1,", "&K$1,""))) = IF(MID(TRIM(IF(A2 = 1,A $ 1,“”)&IF(B2 = 1,“,”&B $ 1,“”)&IF(C2 = 1,“,”&C $ 1,“”)&IF (D2 = 1,“,”&D $ 1,“”)&IF(E2 = 1,“,”&E $ 1,“”)&IF(F2 = 1,“,”&F $ 1,“”)&IF(G2 = 1, “,”&G $ 1,“”)&IF(H2 = 1,“,”&H $ 1,“”)&IF(I2 = 1,“,”&I $ 1,“”)&IF(J2 = 1,“,”&J $ 1 ,“”)&IF(K2 = 1,“,”&K $ 1,“”)),1,2)=“,”,MID(TRIM(IF(A2 = 1,A $ 1,“”)&IF(B2 = 1,“,”&B $ 1,“”)&IF(C2 = 1,“,”&$ C $ 1,“”)&IF(D2 = 1,“,”&D $ 1,“”)&IF(E2 = 1,“ ,“&E $ 1,”“)&IF(F2 = 1,”,“&F $ 1,”“)&IF(G2 = 1,”,“&G $ 1,”“)&IF(H2 = 1,”,“&H $ 1, “”)&IF(I2 = 1,“,”&I $ 1,“”)&IF(J2 = 1,“,”&J $ 1,“”)&IF(K2 = 1,“,”&K $ 1,“”))), 3,1000),TRIM(IF(A2 = 1,A $ 1,“”)&IF(B2 = 1,“,”&B $ 1,“”)&IF(C2 = 1,“,”&C $ 1,“”)&IF (D2 = 1,“,”&D $ 1,“”)&IF(E2 = 1,“,”&E $ 1,“”)&IF(F2 = 1,“,”&F $ 1,“”)&IF(G2 = 1, “,”&G $ 1,“”)&IF(H2 = 1,“,”&H $ 1,“”)&IF(I2 = 1,“,”&I $ 1,“”)&IF(J2 = 1,“,”&J $ 1 ,“”)&IF(K2 = 1,“,”&K $ 1,“”))))

This formula has a limit of 1000 character in column by the mid function. 此公式的中值函数在列中限制为1000个字符。 and it repeats 3 times the same formula because of the separator: ", " but if you dont mind the result format you can short it to one. 并且由于分隔符“,”,它会重复相同公式的3倍,但是如果您不介意结果格式,则可以将其缩写为1。 just fill put 1 in columns and the column name will appear. 只需在第1列中填写1,就会出现列名。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM