如果部分匹配

时间:2018-03-22 22:32:10

标签: excel string-matching partial

A栏:8500个父母SKU号码清单 B栏:8500个儿童SKU号码清单

我需要运行一个公式,检查列B与列A的部分匹配,并列出C列中的单元格数据,

需要在C列中生成几个部分匹配,所有部分匹配都用逗号分隔。

3 个答案:

答案 0 :(得分:2)

我想我知道你要做什么。这个UDF应该按原样处理你的数据。把它放在一个工作簿模块中,然后你就可以调用它(假设你在C2=find_my_children(A2)。(你可以随心所欲地命名,我只是有点乐趣:P )

Function find_my_children(sku As String)
Dim parentRng As Range, childRng As Range
Dim results As String
results = ""

Set parentRng = Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row) ' Change these two as necessary
Set childRng = parentRng.Offset(0, 1)

Dim cel As Range

For Each cel In childRng
    If InStr(1, cel, sku) > 0 And InStr(1, results, cel) = 0 Then
        results = results & cel.Value
    End If
Next cel

If results = "" Then results = "NO DATA FOUND" ' You can comment this out, or change the message as needed.

find_my_children = results

End Function

enter image description here

(我假设您只有一个工作表。如果您有多个工作表,您将需要使用该工作表名称限定范围。尽管无论工作表数量多少都是最佳实践,但为了简化OP I把那部分遗漏了。)

答案 1 :(得分:1)

这似乎是正确的,但目前未经测试,因为我不会从图像中重新输入您的数据。

Option Explicit

Sub collectChildSkus()
    Dim d As Long, dict As Object, skus As Variant

    Set dict = Create("scripting.dictionary")

    With Worksheets(1)
        With .Columns("B").Cells
            .TextToColumns Destination:=.Cells(1), DataType:=xlDelimited, _
                           TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, _
                           Comma:=True, Tab:=False, Semicolon:=False, Space:=False, Other:=False, _
                           FieldInfo:=Array(Array(1, 1), Array(2, 9))
        End With

        vals = .Range(.Cells(2, "A"), .Cells(.Rows.Count, "B").End(xlUp)).Value2

        For d = LBound(vals, 1) To UBound(vals, 1)
            If dict.exists(vals(d, 1)) Then
                dict.Item(vals(d, 1)) = dict.Item(vals(d, 1)) & ", " & vals(d, 2)
            Else
                dict.Item(vals(d, 1)) = vals(d, 2)
            End If
        Next d

        .Cells(2, "C").Resize(dict.Count, 1) = dict.items
    End With
End Sub

答案 2 :(得分:0)

Try a User-Defined Function. Basically it's a Concatenate If hybrid

Function FINDMATCHES(PARENT_RANGE As Range, CHILD_RANGE As Range)

Dim c As Range
Dim answer As String

    For Each c In CHILD_RANGE.Cells
        If c.Offset(, -1).Value = PARENT_RANGE.Value Then answer = answer & " " & c.Value
    Next c

    answer = Trim(answer)
    answer = Replace(answer, " ", ", ")

FINDMATCHES = answer

End Function

enter image description here