如果行中有匹配项,则返回列名,以查找多个匹配项 [英] Returning column names if there is match in the row, looking for multiple matches

查看:116
本文介绍了如果行中有匹配项,则返回列名,以查找多个匹配项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张桌子,桌子的各个部分如下;

I have one table with parts as following;

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

第二个表的列中的数字是单位".
我试图弄清楚如何获得零件带有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                 |
----------------------------------------

现在,我最近刚刚学习了如何使用INDEX和MATCH,但是还没有获得想要的结果.我已经尝试过使用数组公式,但是我还不了解它们.

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.

推荐答案

如果您已订阅Office 365 Excel,则可以使用以下数组公式:

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,""))

作为数组公式,退出编辑模式时,需要使用Ctrl-Shift-Enter而不是Enter进行确认.如果操作正确,则Excel会将{}放在公式周围.

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.

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

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

这篇关于如果行中有匹配项,则返回列名,以查找多个匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆