Userform Textbox validation

bigj4155

Board Regular
Joined
Mar 3, 2005
Messages
187
All I can say is grrrrrrr about this one. All I want to do is check a textbox in a userform for alpha characters. If it does have alpha characters I want to reset the box and prompt to enter a number.

This is a bit of code that I was just messing around with to try and figure it out.

Code:
Private Sub CommandButton1_Click()
Dim t1 As Integer
Dim t2 As Integer
If TextBox1 = "" Or TextBox1 < 1 Then
    MsgBox "Please Enter numbers1"
    Exit Sub
ElseIf TextBox2 = "" Or TextBox2 < 1 Then
    MsgBox "Please enter numbers2"
Exit Sub
End If

    t1 = TextBox1.Value
    t2 = TextBox2.Value
    Worksheets("Sheet1").Range("A20").Value = t1 + t2
End Sub

Basic piece of code but if I could ever figure the $#%@$% checking for alpha character thing it is actually going to be used in a much larger project. ****pulls hair out*****
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Have you considered attacking this issue at the source, that being, only allowing numbers to be entered into the textboxes. Stick these 2 blocks in your userform module and test them by entering number and non-number data:



Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
'Numbers OK
Case 48 To 57
Exit Sub
'Nothing else is acceptable
Case Else
KeyAscii = 0
MsgBox "Only numbers are allowed.", 48, "Numbers only please."
Exit Sub
End Select
End Sub


Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
'Numbers OK
Case 48 To 57
Exit Sub
'Nothing else is acceptable
Case Else
KeyAscii = 0
MsgBox "Only numbers are allowed.", 48, "Numbers only please."
Exit Sub
End Select
End Sub
 
Upvote 0
Private Sub TextBox1_Change()

On Error GoTo myErr
If TextBox1.Value = xlNull Then Exit Sub
If Application.WorksheetFunction.IsNumber(TextBox1.Value * 1) = False Then

myErr:
MsgBox "Only numbers are allowed.", 48, "Numbers only please."
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,225,055
Messages
6,182,595
Members
453,126
Latest member
NigelExcel

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top