R
ranswrt
I have a textbox in a userform that I want to only accept numbers and '/' to
enter dates. How do I do that?
Thanks
enter dates. How do I do that?
Thanks
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
and '/' to enter dates. How do I do that?
Rick Rothstein (MVP - VB) said:I have a textbox in a userform that I want to only accept numbers
and '/' to enter dates. How do I do that?
You can use the code at the bottom of my posting to do what you asked (it
will also stop the user from pasting in text containing characters other
than digits and the slash character). You will still have to validate the
entry after the user has finished typing his/her entry to make sure it is a
valid date (you can use the IsDate function for this).
Just out of curiosity, have you considered using one of the drop-down
calendar controls to take your date input (the input mechanism is much more
natural for the user and it foolproof date-entry-wise)? I use the "Microsoft
Date and Time Picker Control" for this purpose. You can add it to your
UserForm's Toolbox by right-clicking the Toolbox and clicking on "Additional
Controls", then put a checkmark next to the "Microsoft Date and Time Picker
Control" item and click OK. Try it... I think you will like it.
Rick
'***********BEGIN PASTE***********
Dim LastPosition As Long
Private Sub TextBox1_Change()
Static LastText As String
Static SecondTime As Boolean
With TextBox1
If Not SecondTime Then
If .Text Like "*[!0-9/]*" Then
Beep
SecondTime = True
.Text = LastText
.SelStart = LastPosition
Else
LastText = .Text
End If
End If
End With
SecondTime = False
End Sub
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
LastPosition = TextBox1.SelStart
End Sub
'***********END PASTE***********
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.