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

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
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
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,447
Members
448,966
Latest member
DannyC96

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