我想检查放在列表框中的文本的值。如果包含此标志之一,我应该输入错误消息。除了那样检查之外,还有其他方法吗?
If InStr(ListBox.list(i), ";") > 0 Or InStr(ListBox.list(i), "<") Or InStr(ListBox.list(i), ">")
因为我必须检查大约30个标志,所以它会很凌乱。另一个问题是,如何检查包含"
(引号)的内容,因为如果我这样写"""
,它将不起作用。
答案 0 :(得分:1)
请尝试下一个代码。引号构成获得的数组的一部分:
Sub testExistingSigns()
Dim x As String, arrS As Variant, El As Variant, boolFound As Boolean
arrS = Array(";", "<", ">", """", "|", ":")
'or in this way:
arrS = Split(";,<,>,"",|,:", ",") 'I prefer this one. It is shorter...
'you will comment/delete the line which looks less convenient...
'to test the first variant, please comment the second one.
'test it:
Debug.Print Join(arrS, " ")' Look in Immediate Window (Ctrl + G being in VBE)
For Each El In arrS
If InStr(ListBox.List(i), El) > 0 Then
boolFound = True: Exit For
End If
Nex
If boolFound Then Debug.Print "Ups..."
End Sub