两个字符串等于true时为true,但是Like运算符为false

为什么“赞”语句不起作用?

“ =”语句检查为true,但“ like”运算符检查为false,为什么?

Sub sandbox2()

Dim TagForm As String

TagForm = "tag(1###)<EX-->"

Debug.Print "Compare: " & Sheets(2).Cells(2,2).Value & " To: " & TagForm

If Sheets(2).Cells(2,2).Value = TagForm Then 'this works... displays message "Match!"
    MsgBox "Match!"
End If

If Sheets(2).Cells(2,2).Value Like TagForm Then 'this does not work... Does not display "Match!"
    MsgBox "Match!"
End If

End Sub
oldbie 回答:两个字符串等于true时为true,但是Like运算符为false

使用like运算符时,#-都是特殊字符。

要匹配文字字符#,请在其周围加上方括号,例如[#]

在此处查看完整的文档:https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/like-operator

,

您使用了Like operator错误。 #代表一个数字。

因此您可以比较以下内容:

Sub test()
    Dim a As String,b As String,c As String
    a = "tag(1###)<EX-->"
    b = "tag(1###)<EX-->"
    c = "tag(1000)<EX-->"

    Debug.Print b = a       'true
    Debug.Print b Like a    'false

    Debug.Print c = a       'false
    Debug.Print c Like a    'trua
End Sub

如果您比较MyVar LIKE "tag(1###)<EX-->",然后
MyVar可以是"tag(1000)<EX-->""tag(1999)<EX-->"

本文链接:https://www.f2er.com/3165437.html

大家都在问