繁体   English   中英

动态查找单元格中的多个值(逗号分隔)并将相应的 ID 返回到单个单元格(逗号分隔)

[英]Dynamic Lookup for multiple values in a cell (comma separated) and return the corresponding ID to a single cell (comma separated also)

事情并不总是每个单元格内的值(ID)的数量相同(至少1个,最大值=几个),这就是为什么使用串联vlookup+left/mid/right的固定版本对我不起作用的原因是这将解决方案最多只能工作 3 个值。 唯一固定的大小是要查找的值的大小(ID - 绿色),8 个字符(字母+数字)。

我不确定,但是否可以在 excel 公式/函数中设置循环? 下面是一个表格,其中包含我正在尝试解决的问题的示例和预期值(表格位于不同的选项卡中)。 希望你能帮忙。 谢谢。

示例表在此处输入图像描述

如果您有带有TEXTJOINFILTERXML函数的 windows Excel O365,则可以使用公式:

=TEXTJOIN(",",TRUE,IFERROR(XLOOKUP(FILTERXML("<t><s>" & SUBSTITUTE(@[IDs],",","</s><s>") & "</s></t>","//s"),Table2[IDs],Table2[IDv2]),"""--"""))

请注意,在您的数据中, A4中有两个 ID 与表 2 中的任何 ID 都不匹配。虽然这可能是一个错字,但我将它们保留原样以演示错误处理。

表格1 在此处输入图像描述

表2

在此处输入图像描述

这是一个UDF,它将按照您的描述进行。 将代码粘贴到标准代码模块(不是工作簿中已经存在的,而是您创建的,在您将其更改为您最喜欢的名称之前,它的名称类似于Module1 。您还可以重命名 function 以提供更多合适的名字。

Function ID_v2(Cell As Range) As String
    ' 035

    Dim Fun         As String           ' function return value
    Dim Sp()        As String           ' array of CSVs of CellVal
    Dim VLRng       As Range            ' the lookup range
    Dim VL          As Variant          ' result of VLookup
    Dim i           As Integer          ' loop counter

    ' this is a range similar to your sample A10:D19
    Set VLRng = ThisWorkbook.Names("Table2").RefersToRange
    Sp = Split(Cell.Cells(1).Value, ",")
    If UBound(Sp) >= 0 Then
        For i = 0 To UBound(Sp)
            On Error Resume Next
            VL = Application.VLookup(Trim(Sp(i)), VLRng, 3, False)
            If Err Then VL = "[ERROR]"
            Fun = Fun & VL & ","
        Next i
        ID_v2 = Left(Fun, Len(Fun) - 1)      ' remove final comma
    End If
End Function

使用类似内置函数的语法调用 function。 例如,

= ID_v2(A3)

这可以像任何其他 function 一样复制下来。 但请记住将工作簿另存为启用宏。

尝试这个:

Option Explicit

    Sub Cell2List()
        Dim wF As WorksheetFunction: Set wF = Application.WorksheetFunction 'To user Transpose
        Dim i As Range
        Dim j As Range
        Dim s As String: s = "," 'The separator of the list

        'Ask the user for the cell where are the list with the commas
        'Just need to select the cell
        Set i = Application.InputBox("Select just one cell where the values are", "01. Selecte the values", , , , , , 8)

        'Ask the for the separator. If you are completely sure the comma will never change just delete this line
        s = Application.InputBox("Tell me, what is the character separator, just one character! (optional)", "02. Separator (comma semicolon colon or any other char)", , , , , , 2)
        If s = "" Then s = "," 'Verifying...........

        'Ask the user where want to put the list
        'You need to get ready the cells to receive the list.
        'If there any data will be lost, the macro will overwrite anything in the cells
        Set j = Application.InputBox("Select just one cell where the values will go as a list, just one cell!", "03. Selecte the cell", , , , , , 8)

        Dim myArr: myArr = (Split(i.Value, s)) 'Split the list into a Array


        Range(Cells(j.Row, j.Column), Cells(j.Row + UBound(myArr), j.Column)).Value = wF.Transpose(myArr)
        'j.Row is the row of the cell the user selected to put the cell
        'j.Column the same, but the column
        'j.Row + UBound(myArr) =      UBound(myArr) is the total count of elements in the list
        '                            +j.Row
        '                            _______________
        '                            the last cell of the new list!
        'wF.Transpose(myArr) = we need to "flip" the array... Don't worry, but Don't change it!
    End Sub

你可以把这个宏和一个按钮放在丝带上,或者像你在 gif 中看到的那样使用它

在此处输入图像描述

这将是结果:(列表更大)

在此处输入图像描述

编辑

您可以使用此 UDF:

Function Cells2List(List As Range, Pos As Integer) As String
    Cells2List = Split(List, ",")(Pos - 1)
End Function

只需要这样定义和索引:

在此处输入图像描述

告诉function,你想看什么索引。 您可以使用 function 使用ROW()-#在开头和公式发送#VALUE! 删除公式。 $A$1是列表所在的位置, D7是索引所在的位置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM