Calling a sub

B

BOBODD

Someone else posted this really useful code for validating textboxes:

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 48 To 57 'numbers
Case Else 'Discard anything else
Beep
KeyAscii = 0
End Select
End Sub

Only problem is that I have numerous textboxes to check and I've currently
listed this for every box.

How do I package this so that for each textbox I can use something similar to:

Private Sub txtBankNum4_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Call NumCheck
End Sub
 
J

Jim Thomlinson

Try somethin like this...

Private Sub txtBankNum4_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Call NumberValid(KeyAscii)
End Sub

Private Sub NumCheck(ByRef KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 48 To 57 'numbers
Case Else 'Discard anything else
Beep
KeyAscii = 0
End Select
End Sub
 

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