2011-07-19 65 views
12

我想用查找或類似的功能來搜索工作表,匹配帳號,然後返回指定的值。我的問題是有重複的帳戶號碼,我想結果連接到一個字符串的結果。excel與多個結果的查找

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

是在第一個工作表,第二個工作表上我有沒有會計與其他信息,我需要得到所有的匹配結果爲一列2號表即。 「穀物油籽乾草」

+1

+1容易理解的第一個問題:) – ChrisO

回答

7

這是一個可以幫你做的功能。這與Vlookup有一點不同,因爲您只會將它列入搜索列,而不是整個範圍,然後作爲第三個參數,您會告訴它需要向左(負數)還是向右(正數)移動多少列才能獲得你的回報價值。

我還添加了使用分隔符的選項,在您的情況下您將使用「」。下面是函數調用你的,假設第一排,科目編號爲A,結果是B行:

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

下面是函數:

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作爲範圍,因爲我不知道你的範圍,但是 顯然如果你輸入實際的範圍會更快。
+0

作品太棒了!不幸的是,它需要很長的時間:( – javydreamercsw

+0

絕對不是一個好主意,通過這個代碼傳遞一個無界的範圍。如果你傳遞A:A這裏的例子,代碼將檢查所有1,048,576行每次運行。輸入一個實際的範圍,而不是傳入整列。 – Jesse

+0

@aevanko使用WorksheetFunction.CountA(lookup_column)來計數,它更快! –

2

一種方式做,這是使用數組公式的所有比賽的填充到一個隱藏列,然後串聯這些值到你的字符串顯示:

=IFERROR(INDEX(cropTypeValues,SMALL(IF(accLookup=accNumValues,ROW(accNumValues)-MIN(ROW(accNumValues))+1,""),ROW(A1))),"") 
  • cropTypeValues:命名範圍包含作物類型的列表。
  • acc查看:命名範圍包含查找的帳號。
  • accNumValues:命名範圍持有您的帳戶列表 數字。

輸入爲數組公式(Ctrl + Shift + Enter),然後按需要複製下來。

讓我知道你是否需要解釋公式的任何部分。

+0

這是我的首選解決方案,因爲它避免了宏同住時,可以得到禁止。這對於無限數量的比賽來說並不理想,但如果你只希望少數幾場比賽,那很好。 https://www.extendoffice.com/documents/excel/2699-excel-vlookup-find-first-2nd-match.html給出了很好的例子。 – beldaz

0

我剛剛有一個類似的問題,我已經查了很長一段時間類似的解決方案,但沒有真正說服我。要麼你必須編寫一個宏或者一些特殊的函數,但是爲了我的需要,最簡單的解決方案是在例如數據庫中使用一個數據透視表。 Excel中。

如果您從數據創建新的數據透視表,並首先將「Acct No」添加爲行標籤,然後將「CropType」添加爲RowLabel,那麼您將擁有一個非常好的分組,可爲每個帳戶列出所有的作物類型。儘管如此,它不會在單個單元中實現。

-1
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)

查找K1的值的所有出現在範圍A1:A25,並返回從列C的相應值,以逗號分隔。

如果要進行求和值,則可以使用SUMIF,例如

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

在C1至求和值:C25其中在列A中的相應值等於K1的值。

ALL D BEST。

0

這裏是我的代碼比一個Excel VLOOKUP甚至更好,因爲你可以選擇criterie科拉姆,而且可以肯定的隔板(Carriege返回太多)...

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 IsInArray(stringToBeFound As String, arr As Variant) As Boolean 
    IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1) 
End Function