Inputting numbers only in Text Box

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have made a user form that upon data in three text boxes, it closes and the
data is entered in relevant cell son my worksheet. I then have a command
button on the worksheet that re-loads the user form. so far so good

What code do I need to ensure only numeric values are accepted in my 3
textboxes?

...and where would I put this code?
 
Hi,

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

If KeyAscii < 48 Or KeyAscii > 57 Then
KeyAscii = 0
End If

End Sub

should do it..
 
Nicke

I tried the code but it didn't work.

I put in inbetween:-
Private Sub TextBox2_Change()


End Sub

Any suggestions?
 
Hi Anthony:

'This is also a way to test the input.

'/// Place the following on the Sheet

Private Sub CommandButton1_Click()
With UserForm1
.Show
.TextBox1.SelStart = 0
.TextBox1.SelLength = Len(.TextBox1.Text)
.TextBox1.SetFocus
End With
End Sub

'/// This is on the Userform

Private Sub CommandButton1_Click()

If IsNumeric(TextBox1.Text) Then
TextBox1.Text = TextBox1.Text
MsgBox "Your Number is " & TextBox1.Text, , _
"Good Luck TK"
UserForm1.Hide
Else
MsgBox "Please enter a valid Number "
With TextBox1
.SelStart = 0
.SelLength = Len(.Text)
.SetFocus
End With
End If
Exit Sub
End Sub

'Good Luck
TK
 
Hi Anthony:

After re-reading your post, I included code
for the other textboxes.


"/// paste the following to your userform

Private Sub CommandButton1_Click()

If IsNumeric(TextBox1.Text) Then
TextBox1.Text = TextBox1.Text
MsgBox "1 Your Number is " & TextBox1.Text, , _
"Good Luck TK"
'UserForm1.Hide
Else
MsgBox "1 Please enter a valid Number "
With TextBox1
.SelStart = 0
.SelLength = Len(.Text)
.SetFocus
End With
Exit Sub
End If

If IsNumeric(TextBox2.Text) Then
TextBox2.Text = TextBox2.Text
MsgBox "2 Your Number is " & TextBox2.Text, , _
"Good Luck TK"
'UserForm1.Hide
Else
MsgBox "2 Please enter a valid Number "
With TextBox2
.SelStart = 0
.SelLength = Len(.Text)
.SetFocus
End With
Exit Sub
End If

If IsNumeric(TextBox3.Text) Then
TextBox3.Text = TextBox3.Text
MsgBox "3 Your Number is " & TextBox3.Text, , _
"Good Luck TK"
UserForm1.Hide
Else
MsgBox "3 Please enter a valid Number "
With TextBox3
.SelStart = 0
.SelLength = Len(.Text)
.SetFocus
End With
End If
Exit Sub
End Sub

Good Luck
TK
 

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