Vlookup, return multiple values to a cell

2020-05-03 12:05发布

is there anyway to return multiple values from a vlookup? I would like col I in Sheet 1 to return multiple values to a cell, or is there another way of displaying this (would rather not pivot)?

Sheet 1 : has all my unique values (Col F, and returning values in Col I),

Sheet 3: Col A has duplicate string values which correspond to unique strings in Col B which are unique, including blanks.

EDIT

Sheet 1 or desired result :

enter image description here

Sheet 1: Current

enter image description here

Sheet 3 Current:

enter image description here

Current formula

=VLOOKUP(F2,Sheet3!A:B,2,FALSE) 

Returns mostly 0's, due to the blanks or multiple values corresponding to the unique values.

标签: excel vlookup
1条回答
等我变得足够好
2楼-- · 2020-05-03 12:36

In terms of VBA then, you have to change the code a bit from what was in the link I sent you. This should work:

Option Explicit
Function vlookupmulti(rngLookup As Variant, rngSource As Range, col As Double) As String
Dim d As Double, strCell As String

'Error if range has less columns than col
If rngSource.Columns.Count < col Then
    vlookupmulti = CVErr(xlErrNA)
    Exit Function
End If

'Loop through rows in the lookup column
For d = rngSource.Row To rngSource.Rows.Count
    If rngLookup = Sheets(rngSource.Parent.Name).Cells(d, rngSource.Column).Value Then
        strCell = Sheets(rngSource.Parent.Name).Cells(d, rngSource.Column + col - 1).Value
        If Len(strCell) > 0 Then vlookupmulti = vlookupmulti & strCell & ", "
    End If
Next d

'Remove comma at end
If Right(vlookupmulti, 2) = ", " Then
    vlookupmulti = Left(vlookupmulti, Len(vlookupmulti) - 2)
End If

'Give error if no results
If vlookupmulti = "" Then
    vlookupmulti = CVErr(xlErrNA)
End If

End Function
查看更多
登录 后发表回答