为什么“赞”语句不起作用?
“ =”语句检查为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
答案 0 :(得分:1)
使用like
运算符时,#
和-
都是特殊字符。
要匹配文字字符#
,请在其周围加上方括号,例如[#]
在此处查看完整的文档:https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/like-operator
答案 1 :(得分:1)
您使用了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-->"