gpt4 book ai didi

vba - 尽 pipe 字符串有效,Excel VBA InStr 返回 0

转载 作者:行者123 更新时间:2023-12-02 14:24:53 25 4
gpt4 key购买 nike

在此代码中,我尝试检查用户输入的输入(txtList)是否可以在数据列表(txtList)中找到。以下代码返回 0(虽然“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)

最佳答案

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

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。

关于vba - 尽 pipe 字符串有效,Excel VBA InStr 返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46346840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com