Textbox Date Validation

proctk

Well-known Member
Joined
Dec 24, 2004
Messages
839
I need some code to validate how the date is entered in textbox on a userform.

Date formate must be dd-mmm-yy
 

Some videos you may like

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,056
Office Version
  1. 365
Platform
  1. Windows
protck

Why not just check a valid date has been entered and convert it required format?

I'm sure you could write code to check the format but I personally wouldn't do things that way.

For one thing you are restricting the user.
 

HalfAce

MrExcel MVP
Joined
Apr 6, 2003
Messages
9,453
Hello Kevin,
Something like this should get you started.
Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Not IsDate(TextBox1.Text) Then
    TextBox1.Value = ""
    Ln1 = "There was as error in the date entry." & vbNewLine
    Ln2 = "Please enter a valid date."
    Style = vbOKOnly
    Title = "Date entry error"
    MsgBox Ln1 & Ln2, Style, Title
  Else: TextBox1.Text = Format(TextBox1.Text, "dd-mmm-yy")
End If
End Sub
Hope it helps.

[EDIT:]
Pretty much what Norie is suggesting I guess.
 

Watch MrExcel Video

Forum statistics

Threads
1,118,335
Messages
5,571,600
Members
412,407
Latest member
ElmerCC
Top