VBA逗号分隔查找

时间:2018-12-18 16:55:28

标签: excel vba concatenation

我正在寻找使用公式或VBA在J列中查找与相同ID号相关联的值,并将其放入A列中以“ Master”开头(以逗号分隔)的任何值。

在下面的示例中,我希望单元格J2返回 “ D,G,R,N”,如果有重复项,则仅列出一次。 J列中的值当前正在使用公式从另一个列表中查找值:

=IFERROR(IF(ISNUMBER(MATCH(M2,Sheet1!$J:$J,0)),"N", 
IF(ISNUMBER(MATCH(M2,Sheet1!$K:$K,0)),"D", 
IF(ISNUMBER(MATCH(M2,Sheet1!$L:$L,0)),"R", 
IF(ISNUMBER(MATCH(M2,Sheet1!$M:$M,0)),"G", 
IF(ISNUMBER(MATCH(M2,Sheet1!$N:$N,0)),"F",""))))), "")

Master Image

1 个答案:

答案 0 :(得分:1)

您可以使用此UDF。由于您要查找公式,因此请将其放在J列的空白单元格中。

=get_areas(C2)

然后将其添加到工作簿模块中

Function get_areas(ID As String) As String
Dim rng As Range, cel As Range

Set rng = Range("A2:A" & Cells(rows.count,1).End(xlUp).Row)

Dim areas As String
For Each cel In rng
    If IsNumeric(Left(cel, 1)) And cel.Offset(0, 2) = ID Then
        If InStr(1, areas, cel.Offset(0, 9)) = 0 Then
            areas = cel.Offset(0, 9) & ", " & areas
        End If
    End If
Next cel

areas = Trim(Left(areas, Len(areas) - 2))
get_areas = areas
End Function

enter image description here