[英]Out of Range error in Excel VBA when trying to access array element that doesn't exist
[英]Excel VBA - Show error message if textbox value doesn't exist in range
我在用户窗体上有一个文本框,该文本框从条形码扫描仪获取其值。
不幸的是,有时光标会跳过扫描的一半而仅输入名称的一部分。
有没有一种方法可以验证完整扫描是否已完成。
我可以在后台有一张包含所有代码的完整列表的工作表,以便可以验证是否更容易?
任何帮助,将不胜感激。
谢谢Al
您可以尝试这样的事情:
Dim ScannedString As String
Scannedstring = Textbox1.Text
Dim StringFound as String
'Range of all Codes
Dim CodeRng as Range 'change this to whatever your list range is ofcourse
set CodeRng = ThisWorkbook.Sheets("Sheet1").Range("A1:A10")
For each code in CodeRng
If code = ScannedString Then
StringFound = code
Exit For
'The scanned code is found in the list so nothing is wrong
End If
Next code
If StringFound = "" Then
MsgBox "The code you scanned does not exist on the list. Please Scan again."
End If
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.