Alan Treanor
Alan Treanor

Reputation: 159

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

Upvotes: 1

Views: 2689

Answers (1)

Goos van den Bekerom
Goos van den Bekerom

Reputation: 1493

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

Upvotes: 2

Related Questions