excel与多个结果的查找

我正在尝试使用vlookup或类似的function来search工作表,匹配帐号,然后返回指定的值。 我的问题是有重复的帐户号码,我想结果连接到一个string的结果。

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

是在第一张工作表中,在第二张工作表上我有科学否与其他信息,我需要获得所有匹配的结果到第二张工作表即一列。 “粮油籽干草”

这是一个可以为你做的function。 这与Vlookup有一点不同,因为你只会给它search列,而不是整个范围,那么作为第三个参数,你会告诉它要走多less列(负数)或右(正数)才能得到你的回报价值。

我还添加了使用分隔符的选项,在你的情况下,你将使用“”。 这里是你的函数调用,假设第一行的帐号是A,结果是B行:

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

这是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 

笔记:

  • 如果你没有input的话,我把“,”作为结果的默认分隔符。
  • 如果有一个或多个命中,我在最后添加了一些检查,以确保string不会以一个额外的分隔符结束。
  • 我已经使用A:A作为范围,因为我不知道你的范围,但是显然如果你input实际的范围会更快。

一种方法是使用数组公式将所有匹配项填充到隐藏列中,然后将这些值连接到您的string中进行显示:

 =IFERROR(INDEX(cropTypeValues,SMALL(IF(accLookup=accNumValues,ROW(accNumValues)-MIN(ROW(accNumValues))+1,""),ROW(A1))),"") 
  • cropTypeValues :命名范围,包含裁剪types的列表。
  • acc查看 :命名范围持有帐号查找。
  • accNumValues :命名范围,包含您的账号的列表。

input数组公式(Ctrl + Shift + Enter),然后根据需要复制下来。

让我知道如果你需要解释公式的任何部分。

我刚刚有类似的问题,我已经查了很长时间类似的解决scheme,但没有真正说服我。 要么你必须编写一个macros,或者一些特殊的函数,但是,为了我的需要,最简单的解决scheme是在Excel中使用数据透视表。

如果你从你的数据创build一个新的数据透视表,并首先添加“Acct No”作为行标签,然后添加“CropType”作为RowLabel,你将有一个非常好的分组,列出了每个帐户所有的作物types。 尽pipe如此,它不会在单个单元中完成。

这里是我的代码,甚至比Excel的查找更好,因为你可以select标准列,并确保一个分隔符(卡里格也回报)…

 Function Lookup_concat(source As String, tableau As Range, separator As String, colSRC As Integer, colDST As Integer) As String Dim i, y As Integer Dim result As String If separator = "CRLF" Then separator = Chr(10) End If y = tableau.Rows.Count result = "" For i = 1 To y If (tableau.Cells(i, colSRC) = source) Then If result = "" Then result = tableau.Cells(i, colDST) Else result = result & separator & tableau.Cells(i, colDST) End If End If Next Lookup_concat = result End Function 

还有一个礼物,你也可以在同一个单元格的多个元素上进行查找(基于同一个分隔符)。 真的很有用

 Function Concat_Lookup(source As String, tableau As Range, separator As String, colSRC As Integer, colDST As Integer) As String Dim i, y As Integer Dim result As String Dim Splitted As Variant If separator = "CRLF" Then separator = Chr(10) End If Splitted = split(source, separator) y = tableau.Rows.Count result = "" For i = 1 To y For Each word In Splitted If (tableau.Cells(i, colSRC) = word) Then If result = "" Then result = tableau.Cells(i, colDST) Else Dim Splitted1 As Variant Splitted1 = split(result, separator) If IsInArray(tableau.Cells(i, colDST), Splitted1) = False Then result = result & separator & tableau.Cells(i, colDST) End If End If End If Next Next Concat_Lookup = result End Function 

以前的子需要这个function

 Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1) End Function 
 Function VLookupAll(vValue, rngAll As Range, iCol As Integer, Optional sSep As String = ", ") Dim rCell As Range Dim rng As Range On Error GoTo ErrHandler Set rng = Intersect(rngAll, rngAll.Columns(1)) For Each rCell In rng If rCell.Value = vValue Then VLookupAll = VLookupAll & sSep & rCell.Offset(0, iCol - 1).Value End If Next rCell If VLookupAll = "" Then VLookupAll = CVErr(xlErrNA) Else VLookupAll = Right(VLookupAll, Len(VLookupAll) - Len(sSep)) End If ErrHandler: If Err.Number <> 0 Then VLookupAll = CVErr(xlErrValue) End Function 

像这样使用:

=VLookupAll(K1, A1:C25, 3)

在范围A1:A25中查找所有出现的K1的值,并从列C返回相应的值,用逗号分隔。

如果你想总结数值,你可以使用SUMIF

=SUMIF(A1:A25, K1, C1:C25)

将C1:C25中的值相加,其中列A中的相应值等于K1的值。

ALL D BEST。