简体   繁体   English

Excel VBA-如果文本框值在范围内不存在,则显示错误消息

[英]Excel VBA - Show error message if textbox value doesn't exist in range

I have a textbox on a userform that gets its value from a barcode scanner. 我在用户窗体上有一个文本框,该文本框从条形码扫描仪获取其值。
Unfortunately sometimes the cursor skips half way through the scan and only enters part of the name. 不幸的是,有时光标会跳过扫描的一半而仅输入名称的一部分。
Is there a way to validate that the full scan has completed. 有没有一种方法可以验证完整扫描是否已完成。

I can have a sheet in the background with a full list of all the codes so could validate against if this makes it easier? 我可以在后台有一张包含所有代码的完整列表的工作表,以便可以验证是否更容易?

Any help would be appreciated. 任何帮助,将不胜感激。

Thanks Al 谢谢Al

You could try something like this: 您可以尝试这样的事情:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM