ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Inputting numbers only in Text Box (https://www.excelbanter.com/excel-programming/313639-inputting-numbers-only-text-box.html)

Anthony Slater

Inputting numbers only in Text Box
 
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?

Nicke[_7_]

Inputting numbers only in Text Box
 

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..

--
Nick
-----------------------------------------------------------------------
Nicke's Profile: http://www.excelforum.com/member.php...nfo&userid=293
View this thread: http://www.excelforum.com/showthread.php?threadid=26952


Anthony Slater

Inputting numbers only in Text Box
 
Nicke

I tried the code but it didn't work.

I put in inbetween:-
Private Sub TextBox2_Change()


End Sub

Any suggestions?

"Nicke" wrote:


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
------------------------------------------------------------------------
Nicke's Profile: http://www.excelforum.com/member.php...fo&userid=2930
View this thread: http://www.excelforum.com/showthread...hreadid=269520



Nicke[_13_]

Inputting numbers only in Text Box
 

Look at my code, its in the *KEYPRESS * event :cool:


--
Nicke
------------------------------------------------------------------------
Nicke's Profile: http://www.excelforum.com/member.php...fo&userid=2930
View this thread: http://www.excelforum.com/showthread...hreadid=269520


TK

Inputting numbers only in Text Box
 
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

TK

Inputting numbers only in Text Box
 
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


"TK" wrote:

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



All times are GMT +1. The time now is 05:40 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com