Limit input in a textbox

J

James Agostinho

Hello NG,
I need to limit the input in a text box via the Key_Press event in VBA.
I'm accepting input for hexadecimal to be converted to Decimal and Binary
but I only the user to be able to input 0-9,a-f and A-F.
I can get a limit with using
If KeyAscii <48 OR KeyAscii > 57 then KeyAscii = 0
This works but only for one set, either numbers or small letter or Caps, but
not all.
Does anyone know how to make a set of character to use, something like
(0123456789abcdefABCDEF)
Any help would be greatly appreciated.
Thanks
Jim
 
A

acw

Jim

Try
Private Sub TextBox1_KeyPress(ByVal KeyAscii As
MSForms.ReturnInteger)
Select Case KeyAscii
Case 48 To 57, 65 To 70, 97 To 102

Case Else
KeyAscii = 0
End Select

End Sub


Tony
 
J

James Agostinho

ACW and Robin,
Thanks for the tips, I got it to work with the CASE version, but I have
another text box that the InStr ver will work with better.
Thanks again, you saved me a big headache ;-)

Jim
 

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