简体   繁体   中英

Create string in same cell if partial match

Column A: List of 8500 Parent SKU numbers Column B: List of 8500 child SKU numbers

I need to run a formula that checks column B against column A for a partial match, and lists the cell data in column C,

There are several partial matches that need to be generated in column C, all seperated by a comma.

I think I see what you're trying to do. This UDF should do the trick with your data as-is. Put this in a workbook module and you can call it (say you're in C2 , with =find_my_children(A2) . (You can name it whatever you want, I just had a bit of fun with it :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

在此处输入图片说明

(I assume you only have one worksheet. If you have multiple sheets, you'll want to qualify the ranges with that worksheet name. It's best practice though regardless of number of sheets, but for simplicity for OP I left that part out.)

This seems right but is currently untested as I was not going to retype your data from an image.

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

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM