【发布时间】:2019-11-04 15:54:52
【问题描述】:
为什么“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
【问题讨论】:
-
对不起...单元格(2,2)。值是“标签(1###)
” -
#表示使用Like时的单个数字。 -
# 是 like 运算符中的特殊字符,代表任何单个数字。因此是假值。将它们括在括号中以逃避其目的
-
因为字面量
#不是数字。 -
将其归结为一个非常的小例子,并在即时窗口
? "#" Like "#"中进一步解释我之前的评论。结果是False。要获取True,请添加括号:? "#" Like "[#]"。
标签: excel vba vb-like-operator