excel vlookup具有多个结果 [英] excel vlookup with multiple results

查看:159
本文介绍了excel vlookup具有多个结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用vlookup或类似功能来搜索工作表,匹配帐号,然后返回指定的值。我的问题是有重复的帐号,我希望结果将结果连接成一个字符串。

I am trying to use a vlookup or similar function to search a worksheet, match account numbers, then return a specified value. My problem is there are duplicate account numbers and I would like the result to concatenate the results into one string.

Acct No   CropType
-------   ---------
0001      Grain
0001      OilSeed
0001      Hay
0002      Grain  

在第一个工作表中,在第二个工作表上,我有Acct No与其他信息,我需要获得所有匹配的结果第二个工作表上的一列,即Grain Oilseed Hay

Is in the first worksheet, on the 2nd worksheet I have the Acct No with other information and I need to get all the matching results into one column on the 2nd worksheet ie. "Grain Oilseed Hay"

任何帮助都不胜感激。

谢谢!

推荐答案

这是一个可以为您做的功能。它与Vlookup有点不同,因为你只会给它搜索列,而不是整个范围,然后作为第三个参数,你会告诉它有多少列左(负数)或右(正)为了得到您的返回值。

Here is a function that will do it for you. It's a little different from Vlookup in that you will only give it the search column, not the whole range, then as the third parameter you will tell it how many columns to go left (negative numbers) or right (positive) in order to get your return value.

我还添加了使用分隔符的选项,在您的情况下,您将使用。这是函数调用,假设第一行是Acct No.是A,结果是行B:

I also added the option to use a seperator, in your case you will use " ". Here is the function call for you, assuming the first row with Acct No. is A and the results is row B:

=vlookupall("0001", A:A, 1, " ")

这是功能: / p>

Here is the function:

Function VLookupAll(ByVal lookup_value As String, _
                    ByVal lookup_column As range, _
                    ByVal return_value_column As Long, _
                    Optional seperator As String = ", ") As String

Dim i As Long
Dim result As String

For i = 1 To lookup_column.Rows.count
    If Len(lookup_column(i, 1).text) <> 0 Then
        If lookup_column(i, 1).text = lookup_value Then
            result = result & (lookup_column(i).offset(0, return_value_column).text & seperator)
        End If
    End If
Next

If Len(result) <> 0 Then
    result = Left(result, Len(result) - Len(seperator))
End If

VLookupAll = result

End Function

注意:


  • 我做了,如果你没有输入,结果的默认分隔符。

  • 如果有一个或多个命中,我添加了一些检查结束
    确保字符串不以额外的分隔符结尾。

  • 我使用A:A作为范围,因为我不知道你的范围,但
    显然如果输入实际范围,速度会更快。

这篇关于excel vlookup具有多个结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆