簡體   English   中英

Excel UDF - 具有部分匹配的VLOOKUP並在單個單元格中連接結果

[英]Excel UDF - VLOOKUP with partial match and concatenate results in single cell

我有2列數據和第三列查詢字詞。 查詢項在數據列1(“Data1”)中具有部分但完全匹配。 我想在“Data1”列中找到查詢字詞的所有匹配項,並從“Data2”列返回逗號分隔值。

請看圖像。

在這個網站上已經存在類似問題的UDF,但這並不是我想要的。

Excel:在進行部分匹配時,在單個單元格中返回多個匹配項

非常感謝你的幫助。 祝福Manoj 數據圖像

我試過這個UDF:

Public Function ConcatPartLookUp(rngInput As Range, rngSource As Range, Optional strDelimiter As String, Optional blCaseSensitive)
Dim rng As Range

If strDelimiter = "" Then strDelimiter = "|"
If IsMissing(blCaseSensitive) Then
    blCaseSensitive = False
Else
    blCaseSensitive = True
End If

For Each rng In rngSource
    If blCaseSensitive Then
        If InStr(1, rng.Value, rngInput.Value, vbBinaryCompare) > 0 Then ConcatPartLookUp = ConcatPartLookUp & strDelimiter & rng.Value
    Else
        If InStr(1, rng.Value, rngInput.Value, vbTextCompare) > 0 Then ConcatPartLookUp = ConcatPartLookUp & strDelimiter & rng.Value
    End If
Next

If Len(ConcatPartLookUp) > 0 Then ConcatPartLookUp = Mid(ConcatPartLookUp, 2, Len(ConcatPartLookUp))

End Function

此代碼將返回“Data1”列(非常藍天。|藍天永遠。)的匹配。 我希望匹配Data1但返回Data2的值。 謝謝

如果您有Office 365 Excel,則可以將TEXTJOIN用作數組公式:

 =TEXTJOIN(",",TRUE,IF(ISNUMBER(SEARCH(D2,$A$2:$A$6)),$B$2:$B$6,""))

作為數組公式,需要在退出編輯模式時使用Ctrl-Shift-Enter而不是Enter來確認。


如果您沒有Office 365,則將此代碼放入模塊中,然后使用上述公式:

Function TEXTJOIN(delim As String, skipblank As Boolean, arr)
    Dim d As Long
    Dim c As Long
    Dim arr2()
    Dim t As Long, y As Long
    t = -1
    y = -1
    If TypeName(arr) = "Range" Then
        arr2 = arr.Value
    Else
        arr2 = arr
    End If
    On Error Resume Next
    t = UBound(arr2, 2)
    y = UBound(arr2, 1)
    On Error GoTo 0

    If t >= 0 And y >= 0 Then
        For c = LBound(arr2, 1) To UBound(arr2, 1)
            For d = LBound(arr2, 1) To UBound(arr2, 2)
                If arr2(c, d) <> "" Or Not skipblank Then
                    TEXTJOIN = TEXTJOIN & arr2(c, d) & delim
                End If
            Next d
        Next c
    Else
        For c = LBound(arr2) To UBound(arr2)
            If arr2(c) <> "" Or Not skipblank Then
                TEXTJOIN = TEXTJOIN & arr2(c) & delim
            End If
        Next c
    End If
    TEXTJOIN = Left(TEXTJOIN, Len(TEXTJOIN) - Len(delim))
End Function

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM