两个字符串检查是否相等但检查是否为 Like 运算符
Two strings check true for equal but check false for Like operator
为什么 "Like" 语句不起作用?
“=”语句检查为真,但 "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
当使用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-->"
之间的任何值
为什么 "Like" 语句不起作用?
“=”语句检查为真,但 "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
当使用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-->"