Userform input question

  • Thread starter Thread starter Anthony
  • Start date Start date
A

Anthony

Hi,

Is there a way to 'force' a user to input the time on my userform in the
format xx:xx, ie 24hrs with ':' between the hours and minutes
for example......

12:23 is good
09:12 is good
8:15 is good
1223 is bad
0912 is bad
815 is bad

the txtbox for this on the userform are txttime and txtATA

if the wrong format is used I would ideally like a msgbox to appear to
advise wrong format used.

hope you can help
thanks
 
Hi,

Try this

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Me.TextBox1.Value = Format(Me.TextBox1.Value, "hh:mm")
End Sub

Mike
 
Why not just put the colon in for them? If your TextBox is named TextBox1,
then this will take the assign the time value either way it is entered...

TheTimeValue = Format(Replace(TextBox1.Value, ":", ""), "00:00")

More important, I would think, is to make sure there are only numbers and
one colon maximum inputted. Something like this maybe...

If Len(TBvalue) - Len(Replace(TBvalue, ":", "")) > 1 Then
MsgBox "You have too many colons!"
ElseIf Not TBvalue Like "*:##" Then
MsgBox "You do not enough digits after the colon!"
Else
TBvalue = Replace(TBvalue, ":", "")
If TBvalue Like "*[!0-9]*" Then
MsgBox "Enter digits and one colon only!"
ElseIf Len(TBvalue) > 4 Then
MsgBox "You have too many digits!"
Else
TheTimeValue = Format(Replace(TBvalue, ":", ""), "00:00")
End If
End If



Rick
 

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