【问题标题】:Excel UDF - VLOOKUP with partial match and concatenate results in single cellExcel UDF - VLOOKUP 部分匹配并在单个单元格中连接结果
【发布时间】:2019-03-27 21:35:48
【问题描述】:

我有 2 列数据和第三列查询词。查询词在数据的 column1(“Data1”)中有部分但完全匹配的匹配项。我想在“Data1”列中查找查询词的所有匹配项,并从“Data2”列返回逗号分隔值。

请看图片。

这个网站上已经有一个类似问题的 UDF,但这并不完全符合我的要求。

Excel: Return multiple matches in a single cell while doing a partial match

非常感谢您的帮助。祝福 ManojData Image

我已经尝试过这个 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 返回值。 谢谢

【问题讨论】:

  • 嗨,欢迎来到 SO。你试过什么了?在此处发布您的代码,以便我们提供帮助

标签: excel vba user-defined-functions


【解决方案1】:

如果您有 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

【讨论】:

  • 嗨,斯科特。感谢 TEXTJOIN UDF。我现在试过了。不幸的是,它从 Data1 列返回值。但是,我希望在 Data1 上匹配,但从 Data2 返回值。例如,您的公式返回 - “非常蓝天。,永远的蓝天。”而我想要的答案是“Sky1,Sky3”
猜你喜欢
  • 2016-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-18
  • 1970-01-01
相关资源
最近更新 更多