Returning column names if there is match in the ro

2019-03-03 09:59发布

问题:

This question already has an answer here:

  • Concatenate column headers if value in rows below is non-blank 1 answer

I have one table with parts as following;

---------------
| Part number |
---------------
| 123456      |
| 16D345      |
| 16E099      |
| 490586      |
| 970884      |
---------------

And another one like so;

---------------------------------------------------
| Part number | 940822 | 940922 | 170345 | 940222 |
---------------------------------------------------
| 123456      |    X   |        |    X   |    X   |
| 16D345      |    X   |        |    X   |        |
| 16E099      |        |    X   |        |    X   |
| 490586      |    X   |        |    X   |    X   |
| 970884      |        |        |    X   |        |
---------------------------------------------------

The numbers in columns of the second table are 'units'.
I'm trying to figure out how to get all the unit numbers where a part has X. Basically I want to end up with the following;

----------------------------------------
| Part number | Used in                |
----------------------------------------
| 123456      | 940822, 170345, 940222 |
| 16D345      | 940822, 170345         |
| 16E099      | 940922, 940222         |
| 490586      | 940822, 170345, 940222 |
| 970884      | 170345                 |
----------------------------------------

Now I've just recently learned how to use INDEX and MATCH but haven't been able to get the result I want. I've tried using array formulas but I don't understand them yet.

回答1:

If you have a subscription to Office 365 Excel then you can use the following array formula:

=TEXTJOIN(", ",TRUE,IF(($E$2:$H$6 = "X")*($D$2:$D$6=A2),$E$1:$H$1,""))

Being an array formula it needs to be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode. If done correctly then Excel will put {} around the formula.


If you do not have Office 365 then you can put this code in a module attached to the workbook and use the formula as described above:

Function TEXTJOIN(delim As String, skipblank As Boolean, arr)
    Dim d As Long
    Dim c As Long
    Dim arr2()
    Dim t As Long, y As Long
    t = -1
    y = -1
    If TypeName(arr) = "Range" Then
        arr2 = arr.Value
    Else
        arr2 = arr
    End If
    On Error Resume Next
    t = UBound(arr2, 2)
    y = UBound(arr2, 1)
    On Error GoTo 0

    If t >= 0 And y >= 0 Then
        For c = LBound(arr2, 1) To UBound(arr2, 1)
            For d = LBound(arr2, 1) To UBound(arr2, 2)
                If arr2(c, d) <> "" Or Not skipblank Then
                    TEXTJOIN = TEXTJOIN & arr2(c, d) & delim
                End If
            Next d
        Next c
    Else
        For c = LBound(arr2) To UBound(arr2)
            If arr2(c) <> "" Or Not skipblank Then
                TEXTJOIN = TEXTJOIN & arr2(c) & delim
            End If
        Next c
    End If
    TEXTJOIN = Left(TEXTJOIN, Len(TEXTJOIN) - Len(delim))
End Function