如果部分匹配,则在同一单元格中创建字符串
Create string in same cell if partial match
A 列:8500 个父 SKU 编号列表
B列:8500个子SKU编号列表
我需要运行一个公式来检查 B 列与 A 列的部分匹配,并列出 C 列中的单元格数据,
C列需要生成多个部分匹配项,全部用逗号分隔。
我想我明白你想做什么。这个 UDF 应该可以处理您的数据 as-is。把它放在一个工作簿模块中,你可以调用它(假设你在 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
(我假设您只有一个工作表。如果您有多个工作表,您将希望使用该工作表名称来限定范围。尽管不管工作表的数量如何,但最好的做法是OP 的简单性我省略了那部分。)
这似乎是正确的,但目前尚未经过测试,因为我不打算从图像中重新输入您的数据。
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
试试User-Defined函数。基本上它是一个 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
A 列:8500 个父 SKU 编号列表 B列:8500个子SKU编号列表
我需要运行一个公式来检查 B 列与 A 列的部分匹配,并列出 C 列中的单元格数据,
C列需要生成多个部分匹配项,全部用逗号分隔。
我想我明白你想做什么。这个 UDF 应该可以处理您的数据 as-is。把它放在一个工作簿模块中,你可以调用它(假设你在 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
(我假设您只有一个工作表。如果您有多个工作表,您将希望使用该工作表名称来限定范围。尽管不管工作表的数量如何,但最好的做法是OP 的简单性我省略了那部分。)
这似乎是正确的,但目前尚未经过测试,因为我不打算从图像中重新输入您的数据。
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
试试User-Defined函数。基本上它是一个 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