Vlookup, return multiple values to a cell

前端 未结 1 1688
栀梦
栀梦 2021-01-27 10:10

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 ra

相关标签:
1条回答
  • 2021-01-27 10:41

    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
    
    0 讨论(0)
提交回复
热议问题