Vlookup,将多个值返回到单元格

时间:2016-08-08 15:00:59

标签: excel vlookup

无论如何都要从vlookup返回多个值?我想在Sheet 1中将col多个值返回给单元格,或者是否有另一种显示方式(宁愿不转动)?

工作表1 :拥有我所有的唯一值(Col F,并在Col I中返回值),

工作表3 :Col A具有重复的字符串值,这些字符串值对应于Col B中唯一的唯一字符串,包括空格。

修改

第1页或所需结果

enter image description here

第1页:当前

enter image description here

第3页当前

enter image description here

当前公式

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

由于空白或多个值对应于唯一值,因此返回大多为0。

1 个答案:

答案 0 :(得分:1)

就VBA而言,您必须更改我发送给您的链接中的代码。这应该有效:

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