简体   繁体   中英

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. Example- In above Image each row corresponding to col "Tags" contain the column name whose corresponding cell contain "1".

For a 4 column example you can use this:

=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.

The formula relies on 1=TRUE to keep it short. The result each IF is followed by a space. The TRIM gets rid of extra spaces left when no 1 occurs. Finally SUBSTITUTE converts 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.

在此处输入图片说明

couldn't find a non vba solution for concatenating a range. So here is a 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,
  2. criteria = 1 (in your case)
  3. conRange = header range
  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,

You could you use this formula from L2 onwards:

=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. 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. just fill put 1 in columns and the column name will appear.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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