【问题标题】:Matching values in one column with comma separated values in another column and returning the matched value将一列中的值与另一列中的逗号分隔值匹配并返回匹配值
【发布时间】:2020-03-11 08:05:19
【问题描述】:

我有一列的值像

himaanshu
akshay
rahul
hgeet

另一列的值如

axs,fdvf,dasad
axs,fdvf,dasad, himaanshu
axs,fdvf,dasad, akshay
asz,wesd,hgeet

我需要从第 1 列的整个列表中返回第 2 列中每一行的匹配名称

解决方案应该是:

 1. None
 2. himaanshu
 3. akshay
 4. hgeet

谁能帮我用我可以在电子表格中使用的公式来解决这个问题。

【问题讨论】:

    标签: google-apps-script google-sheets spreadsheet google-sheets-api


    【解决方案1】:

    试试下面的:

    Sub test()
    
        Dim str1 As String
        Dim rngToSearch As Range, cell As Range
        Dim LastRowA As Long, LastrowC As Long, i As Long, y As Long
        Dim arr As Variant
    
        With ThisWorkbook.Worksheets("Sheet1")
    
            LastRowA = .Cells(.Rows.Count, "A").End(xlUp).Row
            LastrowC = .Cells(.Rows.Count, "C").End(xlUp).Row
    
            Set rngToSearch = .Range("C2:C" & LastrowC)
    
            For i = 2 To LastRowA
    
                str1 = .Range("A" & i).Value
    
                For Each cell In rngToSearch
    
                    arr = Split(cell.Value, ",")
    
                    For y = LBound(arr) To UBound(arr)
    
                        If Trim(arr(y)) = Trim(str1) Then
                            .Range("B" & i).Value = str1
                        End If
    
                    Next y
    
                Next cell
    
            Next i
    
        End With
    
    End Sub
    

    结果:

    【讨论】:

    • 感谢分享答案。但我只需要返回匹配值进行搜索。喜欢第 2 行 - 无第 3 行 - himaanshu 第 4 行 - akshay 第 5 行 - hgeet
    • @Error 想象一下ABC,CDE,FGH,如果您现在查找AB,即使AB 没有出现在该逗号分隔列表中,它也会匹配。
    • @Pᴇʜ 你在写。我会删除答案,如果我想出一个解决方案,我会回来的
    • @Error1004 与 VBA 很好,但 OP 现在指定了谷歌表格:/
    • 我认为 VBA 已包含在内。我的错!
    【解决方案2】:

    看看这个公式是否有效(在谷歌电子表格中)

    =ArrayFormula(iferror(REGEXEXTRACT(C2:C5, textjoin("|", 1, A2:A5)), "none"))
    

    公式从 C 列中的值中提取 A 列中的任何值

    [

    【讨论】:

      【解决方案3】:

      =VLOOKUP("*"&A1&"*", B1:B4,1,0)

      【讨论】:

      • 您可能会从阅读How to Answer 中受益。仅发布没有解释的代码的答案不是很有用。考虑添加解释以改进您的答案。
      • 您的回答也不能正确反映实际问题。想象一下ABC,CDE,FGH,如果您现在查找AB,即使AB 没有出现在该逗号分隔列表中,它也会匹配。
      猜你喜欢
      • 1970-01-01
      • 2021-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多