Userform - validation

Johnwayne

Board Regular
Joined
Sep 6, 2005
Messages
103
Hi

I have a question..how do you create a validation in userform?
I have a textbox on the userform..
and this texbox is link to A1 (meaning if i key in a number in the textbox, it will flow to cell A1 of my worksheet).

However i need to input a criteria...and my criteria is variable.
Meaning my criteria is based on what i key in, in cell C2.
If i key in 10 in cell C2.
my criteria will cap at 10. meaning any numbers i enter in the textbox that is above 10 will return me an error msg.
If i re-enter cell c2 to 20 - it will then change its cap to 20 (value)

is there any way i can do it?
my current formula is:

Public Function Validate()

If TextBox1.Value > worksheets("Sheet1").range("C2").value Then
MsgBox "Error"
Validate = False
Exit Function
End If

Validate = True
End function

However this formula does not work..please advice
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
Hi, I placed this code in a "CommandButton on the "Userform".
Code:
Private Sub CommandButton1_Click()
If Val(TextBox1.Value) > [c2].Value Then
MsgBox "ERROR"
TextBox1.Value = ""
Else
[a1].Value = TextBox1.Value
TextBox1.Value = ""
End If
End Sub
Regards Mick
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,982
Messages
5,834,688
Members
430,310
Latest member
Excelorate3305

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