VBA以逗号分隔其他单元格中的单元格以获取MATCH值

时间:2019-03-15 01:31:43

标签: excel vba excel-formula

我对VBA比较陌生 我想创建一个MACRO来匹配一个用逗号分隔的单元格中与其他单元格相隔的值,并告诉结果是否存在匹配项。

输入:

   a                         b             
1 abc ,edr,edd,eee          abc

输出:

   a                         b              c             
1 abc ,edr,edd,eee          abc           Match
2 abc ,edr,edd,eee          eef          No Match 

代码:

Function TRACK(pValue As String, pWorking As Range)
    Dim pValue As String
    Dim xResult As String
    pWorking() = Split(pWorking, ",")
    xResult = " "

    For Each pValue In pWorking
        If pWorking = pValue Then
        xResult =  “ Match”
    else
        xResult = “ No Match”
    Next

    ASSEMBLY = xResult
End Function

1 个答案:

答案 0 :(得分:0)

将pWorking拆分为数组后,可以使用工作表的Match函数来确定pValue是否包含在其中。

Function TRACK(pValue As String, pWorking As String)

    Dim vals As Variant

    vals = Split(pWorking, ",")

    If IsError(Application.Match(pValue, vals, 0)) Then
        TRACK = "No Match"
    Else
        TRACK = "Match"
    End If

End Function

请注意使用“智能引号”(例如“ and ”)。它们不是代码中的有效引号。

相关问题