restrict textbox values on a userform

conny2254

Board Regular
Joined
Jun 9, 2009
Messages
248
Is there an easy way to restrict what can be entered into a textbox on userform. such as restrict the textbox to numbers 1-12, and to limit a textbox to letters only and to limit the textbox to only 2 characters?

thank you in advance for any help!
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Not well tested and there's probably an easier/more efficient test, but seems to work.

<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br><br><SPAN style="color:#007F00">'// After setting .MaxLength = 2 for both textboxes...</SPAN><br><br><br><SPAN style="color:#007F00">'// For a one or two-digit number</SPAN><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> TextBox1_Change()<br>    <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> TextBox1.Value = vbNullString _<br>    And <SPAN style="color:#00007F">Not</SPAN> TextBox1.Value <SPAN style="color:#00007F">Like</SPAN> "#" _<br>    And <SPAN style="color:#00007F">Not</SPAN> TextBox1.Value <SPAN style="color:#00007F">Like</SPAN> "##" _<br>    Or _<br>    ((<SPAN style="color:#00007F">Not</SPAN> TextBox1.Value > 0 _<br>    <SPAN style="color:#00007F">Or</SPAN> <SPAN style="color:#00007F">Not</SPAN> TextBox1.Value < 13) _<br>    And <SPAN style="color:#00007F">Not</SPAN> TextBox1.Value = vbNullString) <SPAN style="color:#00007F">Then</SPAN><br>        TextBox1.Value = vbNullString<br>        MsgBox "Enter an Integer from 1 to 12", 0, vbNullString<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><SPAN style="color:#007F00">'// For one or two alpha characters</SPAN><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> TextBox2_Change()<br>    <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> TextBox2.Value = vbNullString _<br>    And <SPAN style="color:#00007F">Not</SPAN> TextBox2.Value <SPAN style="color:#00007F">Like</SPAN> "[A-Z]" _<br>    And <SPAN style="color:#00007F">Not</SPAN> TextBox2.Value <SPAN style="color:#00007F">Like</SPAN> "[a-z]" _<br>    And <SPAN style="color:#00007F">Not</SPAN> TextBox2.Value <SPAN style="color:#00007F">Like</SPAN> "[A-Z][A-Z]" _<br>    And <SPAN style="color:#00007F">Not</SPAN> TextBox2.Value <SPAN style="color:#00007F">Like</SPAN> "[a-z][a-z]" _<br>    And <SPAN style="color:#00007F">Not</SPAN> TextBox2.Value <SPAN style="color:#00007F">Like</SPAN> "[A-Z][a-z]" _<br>    And <SPAN style="color:#00007F">Not</SPAN> TextBox2.Value <SPAN style="color:#00007F">Like</SPAN> "[a-z][A-Z]" <SPAN style="color:#00007F">Then</SPAN><br>        TextBox2.Value = vbNullString<br>        MsgBox "Enter one or two letters", 0, vbNullString<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>

Hope that helps,

Mark
 
Upvote 0
try
Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If TextBox1.Value = "" Then Exit Sub
    If Not Numbers(TextBox1.Value, 1, 12) Then
        MsgBox "Invalid entry"
        Cancel = True
    End If
    If Not Alpha(TextBox1.Value) Then
        MsgBox "Alphabets Only"
        Cancel = True
    End If
    If (Not Alpha(TextBox1.Value)) + (Len(TextBox1.Value)<>2) Then
        MsgBox "2 Alphabets only"
        Cacel = True
    End If
End Sub
 
Function Numbers(ByVal txt As String, myMin As Long, _
        myMax As Long) As Boolean
    Numbers = ((Val(txt) >= myMin) * (Val(txt) <= myMax))
End Function
 
Function Alpha(ByVal txt As String) As Boolean
    Alpha = (Not txt Like "*[!A-Za-z]*")
End Function
 
Upvote 0
Thank you for the quick reply that worked perfectly, but if before the message box appears I try to hide the userform and after they click ok on the message box show the userform the macro does not work when the userform reappears? Do you know why this is?

thank you again for all the help!
 
Upvote 0
Thank you for the quick reply that worked perfectly, but if before the message box appears I try to hide the userform and after they click ok on the message box show the userform the macro does not work when the userform reappears? Do you know why this is?

thank you again for all the help!

Which code are you using?
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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