Limit size of number input

  • Thread starter Thread starter Jock
  • Start date Start date
J

Jock

Hi,
I have a text box on a user form which has been limited to accept only two
digits.
Taking this a step further, can it be coded to only acept numbers between 1
and 14 inclusive? If, for example, a user tried to input 17, could a message
box pop up informing the user of the allowed range?
tia
 
Incorporate this code into the Change event procedure for the TextBox...

If TextBox1.Value <> "" And TextBox1.Value > 14 Then
MsgBox "Number is too large!"
End If

Note that I have assumed your TextBox is named TextBox1 for my example...
change the TextBox references to your actual TextBox names when implementing
this code.
 
Jock,

You could do something like this

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Val(TextBox1) > 14 Or Val(TextBox1) < 1 Or Not IsNumeric(TextBox1) Then
TextBox1.Text = ""
MsgBox "Numbers only between 1 & 14 - Please re-enter"
Cancel = True
End If
End Sub

Mike
 

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

Back
Top