Excel VBA InStr返回0,尽pipe子string是有效的

在这段代码中,我试图检查用户input的input(txtList)是否可以在数据列表(txtList)中find。 下面的代码返回0(没有find子串,虽然“John Ho”和“Ho Tee Nee,John”是完全相同的人。请问有人能告诉我如何解决这个问题?

'code returns 0 (substring not found) Dim txtList As String, txtInput As String txtList = "Ho Tee Nee, John" txtInput = "John Ho" Debug.Print InStr(1, txtList, txtInput, vbTextCompare) 

拆分search条件并查找每个部分。

 Dim i As Long, txtList As String, txtInput As Variant txtList = Chr(32) & "Ho Tee Nee, John" & Chr(32) txtInput = Split("John Ho", Chr(32)) For i = LBound(txtInput) To UBound(txtInput) If Not CBool(InStr(1, txtList, Chr(32) & txtInput(i) & Chr(32), vbTextCompare)) Then Exit For Next i If i > UBound(txtInput) Then Debug.Print "all parts match" Else Debug.Print "incomplete match" End If 

这是一个不区分大小写的search。 对于区分大小写的search,将vbTextCompare更改为vbBinaryCompare。