结束时不带编译错误。我正在比较两个字符串

时间:2018-10-07 14:08:44

标签: excel vba excel-vba

Function CompareTwo(txt As String, txt2 As String, _
            Optional delim As String = ";") As String
    Dim a, b
        With CreateObject("Scripting.Dictionary")
            .CompareMode = vbTextCompare
            For Each a In Split(txt, delim)
                For Each b In Split(txt2, delim)
                 If Trim(a).contains(Trim(b)) Then .Add Trim(a), Nothing
                Next b
            Next a   
    If .Count > 0 Then
    CompareTwo = Join(.keys, delim)

        End With
End Function

1 个答案:

答案 0 :(得分:2)

您缺少结束条件。

Function CompareTwo(txt As String, txt2 As String, _
            Optional delim As String = ";") As String
    Dim a, b
        With CreateObject("Scripting.Dictionary")
            .CompareMode = vbTextCompare
            For Each a In Split(txt, delim)
                For Each b In Split(txt2, delim)
                    ' what is 'contains' ?
                    If Trim(a).contains(Trim(b)) Then .Add Trim(a), Nothing
                    ' maybe this is better
                    If cbool(instr(1, Trim(a), Trim(b), vbtextcompare)) Then .Add Trim(a), Nothing
                Next b
            Next a   
            If .Count > 0 Then
                CompareTwo = Join(.keys, delim)
            end if  '<~~ here

        End With
End Function