Excel VBA仪器返回0,尽管子字符串有效



在此代码中,我正在尝试检查用户(txtlist)输入是否可以在数据列表(TXTLIST)中找到。以下代码返回0(尽管" John Ho"one_answers" 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)

拆分搜索条件并查找每一部分。

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

这是一个不敏感的搜索。对于案例敏感的搜索更改vbtextcompare为vbbinarycompare。

最新更新