在单个单元格中用逗号分隔获取多个查找值

问题描述 投票:0回答:1

我有两个不同的Excel工作表,并试图过滤与sheet2中列出的每个水果相关的所有价格。

Sheet1

enter image description here

Sheet2

enter image description here

您可以看到,sheet2中没有出现橙色价格-12。

预期结果

enter image description here

[LookupCSVResults函数

Option Explicit
Function LookupCSVResults(lookupValue As Variant, lookupRange As Range, resultsRange As Range) As String

    Dim s As String 
    Dim sTmp As String  
    Dim r As Long  
    Dim c As Long   
    Const strDelimiter = "|||" 

    s = strDelimiter
    For r = 1 To lookupRange.Rows.Count
        For c = 1 To lookupRange.Columns.Count
            If lookupRange.Cells(r, c).Value = lookupValue Then
                sTmp = resultsRange.Offset(r - 1, c - 1).Cells(1, 1).Value
                If InStr(1, s, strDelimiter & sTmp & strDelimiter) = 0 Then
                    s = s & sTmp & strDelimiter
                End If
            End If
        Next
    Next

    s = Replace(s, strDelimiter, ",")
    If Left(s, 1) = "," Then s = Mid(s, 2)
    If Right(s, 1) = "," Then s = Left(s, Len(s) - 1)

    LookupCSVResults = s

End Function

任何建议都是可取的。

excel vba vlookup
1个回答
0
投票

没有看到在Sheet2上使用什么公式来获取Oranges的列表,我认为您所做的只是从上面的单元格中复制了公式。这具有将公式引用的单元格向下移动一格的效果。

所以我认为您的Orange公式当前为:

=LookupCSVResults(A2,Sheet1!B3:B10,Sheet1!C3:C10)

因此不会查看第一行数据,这是用于Orange的。

您的公式实际上应该是:

=LookupCSVResults(A2,Sheet1!B2:B9,Sheet1!C2:C9)

将按预期返回“ 12,8,9”。 “桃子”可能会发生类似的情况,但这并未显示为错误。

您可能想使用绝对单元格位置:

=LookupCSVResults(Sheet2!A2,Sheet1!$B$2:$B$9,Sheet1!$C$2:$C$9)

问候,

© www.soinside.com 2019 - 2024. All rights reserved.