Textbox Date Validation

proctk

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

Date formate must be dd-mmm-yy
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
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.
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,735
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top