如果行中有匹配项,则返回列名,查找多个匹配项 [英] Returning column names if there is match in the row, looking for multiple matches
问题描述
我有一张表,其部分如下;
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屋!