Limiting values that can be entered in a form

VBA_Rage

New Member
Joined
Mar 12, 2009
Messages
2
This may sound like a simple question but I really can't find the answer!

How can I restrict the values entered in a user form. Example - I have a text box set up in a user form that prompts for inputs that will only accept numbers - but I want to restrict that further by only allowing the user to enter a number between 10 and 15. I can't use a list because I need the flexibility of having decimal entries as well (e.g. 10.27, 14.49, etc).

Any ideas?
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

Gary McMaster

Well-known Member
Joined
Feb 8, 2009
Messages
1,977
The snip below works for me. Of course you'll probably need to change the name of the textbox.

Hope it helps.

Gary

Code:
Private Sub TextBox1_LostFocus()

If Val(TextBox1.Text) < 10 Or Val(TextBox1.Text) > 15 Then
    MsgBox "Value must be between 10 and 15"
End If

TextBox1.Activate

End Sub
 
Upvote 0

shyrath

Active Member
Joined
Jun 28, 2005
Messages
483
This works for me

Code:
Private Sub TextBox3_Exit(ByVal Cancel As MSForms.ReturnBoolean)

    If TypeName(Me.ActiveControl) = "TextBox" Then

        With Me.ActiveControl

            If Not IsNumeric(.Value) And .Value <> vbNullString Then

                MsgBox "Sorry, only numbers allowed"

                .Value = vbNullString
                
                Else
                
                If .Value > 15 Or .Value < 10 Then
                MsgBox "please enter between 10 and 15"
                .Value = vbNullString
                End If

            End If

        End With

    End If

    '

End Sub
 
Upvote 0

VBA_Rage

New Member
Joined
Mar 12, 2009
Messages
2
Thanks but it doesn't seem to get me any further - I can still input the 'wrong' number in the form and it then throws up the message when I click OK to transfer the input values to the worksheet - and gets stuck in a loop since I can't get away from the message box without it resetting the value in the textbox in the form. what i really need is a message that pops up to tell me its wrong and then drops me back into the box in the form before letting me move onto the next box.
 
Upvote 0

Forum statistics

Threads
1,191,167
Messages
5,985,053
Members
439,936
Latest member
BSR

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
Top