Reputation: 21
I want to make a textbox but that text box only can be input by date format and nothing else.
Upvotes: 1
Views: 41327
Reputation: 23974
As far as I know, you can't (easily) force the text in a TextBox to always be a valid date, but you can check what has been entered as the user tries to leave the TextBox:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Not IsDate(TextBox1.Text) Then
MsgBox "Date required"
Cancel = True
End If
'Display value in another textbox for testing purposes
TextBox2.Text = Format(CDate(TextBox1.Text), "dd/mm/yyyy")
End Sub
Using IsDate
will allow any system-recognised date to be entered, so you should use CDate(TextBox1.Text)
to access the date entered. Don't rely on the text itself being in a particular format because the text entered could be, for instance:
"08/03/2017"
"8 March 2017"
"8 Mar"
"8/3/17"
Upvotes: 4