0

为什么“喜欢”声明不起作用?

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

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
4

2 回答 2

1

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

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

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

于 2019-11-04T16:02:06.447 回答
1

您错误地使用了Like 运算符#代表单个数字。

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

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-->"

于 2019-11-04T16:04:34.670 回答