简体   繁体   English

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

[英]Vlookup, return multiple values to a cell

is there anyway to return multiple values from a vlookup? 反正有从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)? 我希望工作表1中的col I将多个值返回到一个单元格,还是有另一种显示此值的方式(而不是不进行透视)?

Sheet 1 : has all my unique values (Col F, and returning values in Col I), 表格1 :具有我所有的唯一值(Col F和I列中的返回值),

Sheet 3 : Col A has duplicate string values which correspond to unique strings in Col B which are unique, including blanks. 表格3 :A列具有重复的字符串值,这些值与B列中唯一的唯一字符串相对应,包括空白。

EDIT 编辑

Sheet 1 or desired result : 第1张或预期结果

在此处输入图片说明

Sheet 1: Current 表格1:当前

在此处输入图片说明

Sheet 3 Current : 工作表3当前

在此处输入图片说明

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. 由于空白或对应于唯一值的多个值,因此大多返回0。

In terms of VBA then, you have to change the code a bit from what was in the link I sent you. 那么就VBA而言,您必须对我发送给您的链接中的内容进行一些更改。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM