简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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