0

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

表 1

在此处输入图像描述

表 2

在此处输入图像描述

如您所见,Orange price - 12 没有出现在sheet2.

预期结果

在此处输入图像描述

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

任何建议都将是可观的。

4

1 回答 1

1

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

所以我认为你的橙色公式目前是:

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

因此没有查看用于 Orange 的第一行数据。

你的公式实际上应该是:

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

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

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

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

问候,

于 2020-04-25T08:19:42.583 回答