VBA validation rule

emily2309

Board Regular
Joined
Nov 17, 2009
Messages
51
I am trying to create a validation rule for a text box on my user input form. The input form is basically used to add food items to a spreadsheet. The user must enter an amount. I want the amount to be between 1 and 30000, so that if the user adds something outside of this (i.e text or -1), an error message will appear. Please help, i'm not very advanced with VBA :)
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hi Emily

Try;

Code:
[COLOR="Blue"]Private[/COLOR] [COLOR="Blue"]Sub[/COLOR] TextBox1_Exit([COLOR="Blue"]ByVal[/COLOR] Cancel [COLOR="Blue"]As[/COLOR] MSForms.ReturnBoolean)
    [COLOR="Blue"]With[/COLOR] Me.TextBox1
        [COLOR="Blue"]If[/COLOR] .Value < 1 [COLOR="Blue"]Or[/COLOR] .Value > 30000 [COLOR="Blue"]Then[/COLOR]
            [COLOR="Blue"]MsgBox[/COLOR] prompt:="Must be a # between 1 and 30000!", Buttons:=vbCritical, Title:="Invalid Entry"
            Cancel = [COLOR="Blue"]True[/COLOR]
        [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
    [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]With[/COLOR]
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 
Upvote 0
Sorry to be a pain, the box i'm trying to set it to is called TxtAmount, where do i need to add the name?
 
Upvote 0
Anywhere it says TextBox1, replace with TxtAmount.

Code:
Private Sub [COLOR=red]TxtAmount[/COLOR]_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    With Me.[COLOR=red]TxtAmount[/COLOR]
        If .Value < 1 Or .Value > 30000 Then
            MsgBox prompt:="Must be a # between 1 and 30000!", Buttons:=vbCritical, Title:="Invalid Entry"
            Cancel = True
       End If
    End With
End Sub
 
Upvote 0
When the validation box appears, i want the box (TxtAmount) to be blank when i return to the form. Any ideas?
 
Upvote 0
Code:
Private Sub [COLOR=black]TxtAmount[/COLOR]_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    With Me.[COLOR=black]TxtAmount[/COLOR]
        If .Value < 1 Or .Value > 30000 Then
            [COLOR=red].Value=""[/COLOR]
            MsgBox prompt:="Must be a # between 1 and 30000!", Buttons:=vbCritical, Title:="Invalid Entry"
            Cancel = True
       End If
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,929
Messages
6,122,314
Members
449,081
Latest member
tanurai

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