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

我有一个用户表单上的文本框从条形码扫描仪获取它的价值。
不幸的是,有时光标会在扫描过程中跳过一半,只能input部分名称。
有没有办法来validation完整的扫描已经完成。

我可以在背景中填写所有代码的完整列表,这样可以validation,如果这使得它更容易?

任何帮助,将不胜感激。

谢谢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