Check cell value exactly

L

learner

I'm trying to prevent typos entered through Userform:

id = TextBox1.Value
Worksheets("Eq'tDetails").Activate
Set rngToCheck = Columns("A:A")
Set rngValidate = rngToCheck.Find(id, LookIn:=xlValues,
SearchDirection:=xlPrevious)
If rngValidate Is Nothing Then
Unload Me
Worksheets("EntrySheet").Activate
MsgBox ("ID " & id & " Not Found")
GoTo LastLine 'Exits the code
End If
Else
' Rest of the code

I thought I got it but if, for example I accidentally enter "328" or "28" or
"8". etc. in TextBox1 instead of the true number"1328" I'm trying to
validate, Excell still accepts it and proceeds with the rest of the code. It
works fine otherwise.

Thank you for help.
 
J

Jacob Skaria

If you are looking for a whole cell match...

Set rngValidate = rngToCheck.Find(id, LookIn:=xlValues,
LookAt:=xlWhole,SearchDirection:=xlPrevious)

If this post helps click Yes
 
L

learner

Thank you Jacob... so simple, but nevertheless very helpful for an amateur
like me. Very appreciated.
 
P

Per Jessen

Hi

I am not quite sure what you want, but if user should always enter a
four digit number, then try comething like this:

id = TextBox1.Value
If Len(Textbox1.Value)<>4 Or Not IsNumeric(TextBox1.value) then
'Invalud entry
Exit sub
End If
Worksheets("Eq'tDetails").Activate
' Rest of code


Regards,
Per
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top