首页 > 解决方案 > 使用查找表在 Excel 2017/10 中将逗号分隔的 ID 转换为逗号分隔的名称

问题描述

我正在尝试将逗号分隔的 ID 数据与 LOOKUP 表匹配,并将 ID 的名称也以逗号分隔的形式返回。

我基本上是在尝试将 ID 转换为名称

解决方案可以是 VBA 或 Excel 公式,以更容易者为准。我也不是很擅长,但是这个公式是我想出的最好的,虽然你可以看到它有缺少 ID 的问题并且只检查 3 个 ID。

任何帮助将不胜感激,以实现所需的输出结果。

下面代码片段中的示例表

<b></b><table cellpadding="2.5px" rules="all" style=";background-color: rgb(255,255,255);border: 1px solid;border-collapse: collapse; border-color: rgb(187,187,187)"><colgroup><col width="25px" style="background-color: rgb(218,231,245)" /><col /><col /><col /><col /><col /><col /></colgroup><thead><tr style=" background-color: rgb(218,231,245);text-align: center;color: rgb(22,17,32)"><th></th><th>A</th><th>B</th><th>C</th><th>D</th><th>E</th><th>F</th></tr></thead><tbody><tr ><td style="color: rgb(22,17,32);text-align: center;">1</td><td style="font-weight: bold;border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;;">LOOKUP ID</td><td style="font-weight: bold;border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;;">LOOKUP NAME</td><td style="font-weight: bold;text-align: center;border-right: 1px solid black;border-left: 1px solid black;;">*</td><td style="font-weight: bold;border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;;">ID DATA</td><td style="font-weight: bold;border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;background-color: #F2DDDC;;">Formula NAME OUTPUT</td><td style="font-weight: bold;border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;background-color: #EAF1DD;;">Required NAME OUTPUT</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">2</td><td style="border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;;">S2</td><td style="border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;;">ABCDE</td><td style="text-align: center;border-right: 1px solid black;border-left: 1px solid black;;">*</td><td style="border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;;">S2,S6,S7,S4,S3</td><td style="border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;background-color: #F2DDDC;;">ABCDE,ADE,AB</td><td style="border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;background-color: #EAF1DD;;">ABCDE,ADE,AB</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">3</td><td style="border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;;">S3</td><td style="border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;;">AB</td><td style="text-align: center;border-right: 1px solid black;border-left: 1px solid black;;">*</td><td style="border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;;">G2,S4</td><td style="text-align: right;border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;background-color: #F2DDDC;;">#N/A</td><td style="border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;background-color: #EAF1DD;;">D</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">4</td><td style="border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;;">S6</td><td style="border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;;">ADE</td><td style="text-align: center;border-right: 1px solid black;border-left: 1px solid black;;">*</td><td style="border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;;">G9,S6,G2,S3</td><td style="border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;background-color: #F2DDDC;;">GPES,ADE,AB</td><td style="border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;background-color: #EAF1DD;;">GPES,ADE,D,AB</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">5</td><td style="border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;;">S7</td><td style="border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;;">!TE</td><td style="text-align: center;border-right: 1px solid black;border-left: 1px solid black;;">*</td><td style="border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;;">G2,G9</td><td style="border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;background-color: #F2DDDC;;">D,GPES,GPES</td><td style="border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;background-color: #EAF1DD;;">D,GPES</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">6</td><td style="border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;;">G2</td><td style="border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;;">D</td><td style="text-align: center;border-right: 1px solid black;border-left: 1px solid black;;">*</td><td style="border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;;">G2</td><td style="text-align: right;border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;background-color: #F2DDDC;;">#N/A</td><td style="border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;background-color: #EAF1DD;;">D</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">7</td><td style="border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;;">G9</td><td style="border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;;">GPES</td><td style="text-align: center;border-right: 1px solid black;border-left: 1px solid black;;">*</td><td style="border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;;">S2,G9</td><td style="border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;background-color: #F2DDDC;;">ABCDE,GPES,GPES</td><td style="border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;background-color: #EAF1DD;;">ABCDE,GPES</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">8</td><td style="border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;;">&ALL</td><td style="border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;;">ALL</td><td style="text-align: center;border-right: 1px solid black;border-left: 1px solid black;;">*</td><td style="border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;;">&ALL</td><td style="text-align: right;border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;background-color: #F2DDDC;;">#N/A</td><td style="border-top: 1px solid black;border-right: 1px solid black;border-bottom: 1px solid black;border-left: 1px solid black;background-color: #EAF1DD;;">ALL</td></tr></tbody></table><p style="width:4.8em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid rgb(187,187,187);border-top:none;text-align: center;background-color: rgb(218,231,245);color: rgb(22,17,32)">Sheet1</p><br /><br /><table width="85%" cellpadding="2.5px" rules="all" style=";border: 2px solid black;border-collapse:collapse;padding: 0.4em;background-color: rgb(255,255,255)" ><tr><td style="padding:6px" ><b>Worksheet Formulas</b><table cellpadding="2.5px" width="100%" rules="all" style="border: 1px solid;text-align:center;background-color: rgb(255,255,255);border-collapse: collapse; border-color: rgb(187,187,187)"><thead><tr style=" background-color: rgb(218,231,245);color: rgb(22,17,32)"><th width="10px">Cell</th><th style="text-align:left;padding-left:5px;">Formula</th></tr></thead><tbody><tr><th width="10px" style=" background-color: rgb(218,231,245);color: rgb(22,17,32)">E2</th><td style="text-align:left">=VLOOKUP(<font color="Blue">TRIM(<font color="Red">LEFT(<font color="Green">SUBSTITUTE(<font color="Purple">D2,",",REPT(<font color="Teal">" ",20</font>)</font>),20</font>)</font>),$A$2:$B$8,2,FALSE</font>)&","&VLOOKUP(<font color="Blue">TRIM(<font color="Red">MID(<font color="Green">SUBSTITUTE(<font color="Purple">D2,",",REPT(<font color="Teal">" ",20</font>)</font>),20,20</font>)</font>),$A$2:$B$8,2,FALSE</font>)&","&VLOOKUP(<font color="Blue">TRIM(<font color="Red">RIGHT(<font color="Green">SUBSTITUTE(<font color="Purple">D2,",",REPT(<font color="Teal">" ",20</font>)</font>),20</font>)</font>),$A$2:$B$8,2,FALSE</font>)</td></tr></tbody></table></td></tr></table><br />

标签: excelvbaexcel-formula

解决方案


在下面的公式中,我使用Table带有结构化引用的查找表,但如果您愿意,可以将其转换为常规地址。

如果您有带有该SEQUENCE功能的 Excel O365,您可以使用:

=TEXTJOIN(", ",TRUE,IFERROR(INDEX(Table3[NAME],MATCH(TRIM(MID(SUBSTITUTE(D2,",",REPT(" ",99)),IF(SEQUENCE(99)=1,1,(SEQUENCE(99)-1)*99),99)),Table3[ID],0)),""))

如果您有具有该TEXTJOIN功能的 Excel 2016,则可以使用:

=TEXTJOIN(", ",TRUE,IFERROR(INDEX(Table3[NAME],MATCH(TRIM(MID(SUBSTITUTE(D2,",",REPT(" ",99)),IF(seq=1,1,(seq-1)*99),99)),Table3[ID],0)),""))

seqa指的Named Formula是: =ROW(INDEX($A:$A,1):INDEX($A:$A,99))

如果您的早期版本的 Excel 缺少该TEXTJOIN功能,则最好使用 VBA 解决方案,尽管您可以使用多个帮助列来实现。

在此处输入图像描述

对于那些使用早期版本的 Excel 的人,这里有一个应该可以有效工作的 VBA 例程。

由于ID DATA列中最多可能有 100 个匹配项,我认为将查找表读入字典可能会更快,而不是每次都循环遍历数组。(之前的测试表明,对于单个匹配项,循环 VBA 数组比Worksheetfunction.MatchRange.Find方法更快,但我没有针对多个匹配项进行测试。)

Option Explicit

'Go to `tools-->references` in the menu above and
'Set a reference to Microsoft Scripting Runtime for Dictionary

'Since there can be up to 100 items in the list,
'probably faster to read the table into a dictionary
'than to loop through the list for each item

Function getNames(ID_DATA As String, lookup_Table As Range) As String
    Dim D As Dictionary
    Dim I As Long
    Dim S As String
    Dim V As Variant, tbl As Variant

    'delimiter in the output string
    Const sDelim As String = ", "

'read table into dictionary for lookup
tbl = lookup_Table
Set D = New Dictionary
    D.CompareMode = TextCompare
For I = 1 To UBound(tbl)
    'will throw error if there are duplicate ID's
    'but these should be corrected anyway
    D.Add Key:=tbl(I, 1), Item:=tbl(I, 2)
Next I

'create the output string
S = ""
For Each V In Split(ID_DATA, ",")
    S = S & IIf(D(V) = "", "", sDelim & D(V))
Next V

getNames = Mid(S, Len(sDelim) + 1)

End Function

推荐阅读