0

I have two different excel sheets and trying to filter all the prices related to each fruit listed in sheet2.

Sheet1

enter image description here

Sheet2

enter image description here

As you can see, Orange price - 12 is not appearing in the sheet2.

Expected Result

enter image description here

LookupCSVResults function

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

Any suggestions would be appreciable.

1
  • I've just tested your function with your sample data and returned the expected results. What are you entering to your function for lookupRange and outputRange? Commented Apr 25, 2020 at 8:17

1 Answer 1

1

Without seeing what the formula used on Sheet2 to get the list for Oranges, what I think that you have done is just copied down the formula from the cell above. This has the effect of moving the cells that the formula references down by one.

So I think that your formula for Orange is currently:

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

And is therefore not looking at the first row of data, which is for Orange.

Your formula should actually be:

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

which will return "12,8,9" as expected. A similar situation will probably occur for "Peach", but this is not being shown as an error.

You may want to used absolute cell positions:

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

Regards,

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.