[英]Excel formula : Grab the column name corresponding to particular cell
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
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.