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

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,344
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,456
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.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,057
Messages
5,835,163
Members
430,343
Latest member
t0m_c

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
Top