如果行中有匹配项,则返回列名,查找多个匹配项

Returning column names if there is match in the row, looking for multiple matches

我有一个 table,零件如下;

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

还有一个是这样的;

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

第二个table列中的数字是'units'。
我正在尝试弄清楚如何获取零件具有 X 的所有单元编号。基本上我想以以下结尾;

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

现在我最近刚刚学会了如何使用INDEX和MATCH,但一直无法得到我想要的结果。我试过使用数组公式,但我还不明白。

如果您订阅了 Office 365 Excel,那么您可以使用以下数组公式:

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

作为数组公式,退出编辑模式时需要用Ctrl-Shift-Enter确认,而不是Enter。如果正确完成,那么 Excel 将在公式周围放置 {}


如果您没有 Office 365,则可以将此代码放入工作簿附带的模块中并使用上述公式:

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