ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   textbox validation (https://www.excelbanter.com/excel-programming/313347-textbox-validation.html)

TC[_6_]

textbox validation
 
I'm trying to validate user input in a userform textbox, based on a
value in a worksheet cell.
The following sub works fine:

Private Sub TextBoxDay_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If TextBoxDay.Value 0 AND < 32 Then Exit Sub
MsgBox "Valid Days Only"
TextBoxDay = vbNullString
Cancel = True 'Keeps focus until value is null or correct
End Sub


However I cannot get the following code to work:

Private Sub TextBoxDay_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If TextBoxDay.Value 0 AND TextBoxDay.Value < _
Worksheets("Begin").Cells(15, 9) Then Exit Sub
MsgBox "Valid Days Only"
TextBoxDay = vbNullString
Cancel = True 'Keeps focus until value is null or correct
End Sub


I've tried a number of variations but I can't seem to find a cell
reference that works. Worksheets("Begin").Cells(15, 9) = 32.

Thanks for any suggestions

Tim

kkknie[_225_]

textbox validation
 

It's the darned string comparison issue again:

Private Sub TextBoxDay_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If CInt(TextBoxDay.Value) 0 And TextBoxDay.Value < _
CInt(Worksheets("Begin").Cells(15, 9).Value) Then Exit Sub
MsgBox "Valid Days Only"
TextBoxDay = vbNullString
Cancel = True 'Keeps focus until value is null or correct
End Sub

The textbox.value property is a string value. Apparently excel looke
at the Cells().Value property and assumed it was also text.

You may want to do some preliminary checking to make sure the entere
value is numeric before running the compare or it will bomb if an alph
is entered.



--
kkkni
-----------------------------------------------------------------------
kkknie's Profile: http://www.excelforum.com/member.php...nfo&userid=754
View this thread: http://www.excelforum.com/showthread.php?threadid=26871


TC[_6_]

textbox validation
 

Thanks KKKnie. That worked like a charm. I was completely oblivious
to the datatype difference. But I'll certainly watch out for that in
the future.

Tim

On Tue, 12 Oct 2004 19:43:54 -0500, kkknie
wrote:

It's the darned string comparison issue again:

Private Sub TextBoxDay_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If CInt(TextBoxDay.Value) 0 And TextBoxDay.Value < _
CInt(Worksheets("Begin").Cells(15, 9).Value) Then Exit Sub
MsgBox "Valid Days Only"
TextBoxDay = vbNullString
Cancel = True 'Keeps focus until value is null or correct
End Sub

The textbox.value property is a string value. Apparently excel looked
at the Cells().Value property and assumed it was also text.

You may want to do some preliminary checking to make sure the entered
value is numeric before running the compare or it will bomb if an alpha
is entered.

K




All times are GMT +1. The time now is 05:39 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com