VBA Help

jordytheotaku

New Member
Joined
Nov 19, 2010
Messages
2
I am currently creating a userform on microsoft excel using VBA. This userform is for a car company. I have a text box for the mileage of the car, this text box is named txtmileage. I am trying to figure out how to create a character restriction, making it so only numbers are valid inside the text box and for an error message to appear with a small phrase such as "Sorry, numbers only" should anyone attempt to enter any characters which are not numbers.

Thanks for any help that you can provide
~Jordytheotaku
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Try this in the userform module:
Code:
Private Sub txtmileage_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
        KeyAscii = 0
        MsgBox "Numbers only"
    End If
End Sub
 
Upvote 0
Thank you very much for the help John, I am pleased to say that it worked. However, I now have another problem. I was wondering if anybody knows the VBA code that will set a number limit for the mileage, for example, so that the minimum number allowed is around 9000 to 90,000. I appreciate any help that you can give me
 
Last edited:
Upvote 0
Try:
Code:
Private Sub txtmileage_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
        KeyAscii = 0
        MsgBox "Numbers only"
    End If
End Sub

Private Sub txtMileage_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If Len(txtMileage.Text) > 5 Then
        Cancel = True
    ElseIf txtMileage.Value < 9000 Or txtMileage.Value > 90000 Then
        Cancel = True
    End If
    If Cancel Then MsgBox "Mileage must be between 9,000 and 90,000"
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,076
Messages
6,122,983
Members
449,092
Latest member
Mr Hughes

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