leopardhawk
Well-known Member
- Joined
- May 31, 2007
- Messages
- 611
- Office Version
- 2016
- Platform
- Windows
Hello forum friends, ? me after banging my head against the wall!
What is it with Excel and dates???
I have this VBA code for a textbox on a Userform where the user enters their date of birth and the code is supposed to prevent errors in the textbox. I thought everything was working great until I accidentally typed July 2, 194 inadvertently omitting the final digit which was supposed to be a '9' (1949). When I tabbed away to the next textbox, I noticed that the DOB textbox had 7/2/194 in it which it shouldn't because of the code plus, it will be sent to the worksheet when the user selects the OK commandbutton which will create another suite of problems. There was no MsgBox either advising me to "Please enter the year as four digits.".
I am hopeful that someone can help me sort this out, why is it accepting a three-digit year??? Thanks!
What is it with Excel and dates???
I have this VBA code for a textbox on a Userform where the user enters their date of birth and the code is supposed to prevent errors in the textbox. I thought everything was working great until I accidentally typed July 2, 194 inadvertently omitting the final digit which was supposed to be a '9' (1949). When I tabbed away to the next textbox, I noticed that the DOB textbox had 7/2/194 in it which it shouldn't because of the code plus, it will be sent to the worksheet when the user selects the OK commandbutton which will create another suite of problems. There was no MsgBox either advising me to "Please enter the year as four digits.".
I am hopeful that someone can help me sort this out, why is it accepting a three-digit year??? Thanks!
VBA Code:
Private Sub DOBTextBox_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim dt As Date, tx As String
With DOBTextBox
If .Value = "" Then Exit Sub
If IsDate(.Value) Then
tx = .Value
dt = Format(.Value, "d/m/yyyy")
If dt > Date Then
MsgBox "Please enter the year as four digits."
Cancel = True
Else
.Value = dt
End If
Else
MsgBox "Please enter a valid date!"
Cancel = True
.Value = Empty
End If
End With
End Sub