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

时间:2014-09-26 13:39:42

标签: excel vba

我在userform上有一个文本框,可以从条形码扫描仪获取其值 不幸的是,有时光标会在扫描中间跳过一半,只输入部分名称 有没有办法验证完整扫描是否已完成。

我可以在后台找到一张包含所有代码的完整列表,因此如果这样可以更容易验证吗?

任何帮助都将不胜感激。

由于 人

1 个答案:

答案 0 :(得分:2)

您可以尝试这样的事情:

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