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
答案 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