textbox validation

  • Thread starter Thread starter TC
  • Start date Start date
T

TC

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
 
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.
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top