首页 > 解决方案 > 以没有编译错误结束。我正在比较两个字符串

问题描述

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

标签: vbaexcel

解决方案


你错过了一个 End If。

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

推荐阅读