Hello
Have an issue. when entering wrong date ie 30-2-19 (30-Feb-2019) in txtDate.text it displays as 19-Feb-1930. i dont know why.
Infact the below code has the msg of Invalid Date. but it by passes and shows wrong date altogether. The coding should not display any wrong dates
SamD
Have an issue. when entering wrong date ie 30-2-19 (30-Feb-2019) in txtDate.text it displays as 19-Feb-1930. i dont know why.
Infact the below code has the msg of Invalid Date. but it by passes and shows wrong date altogether. The coding should not display any wrong dates
Code:
Option Explicit
Dim disableEvents As Boolean
Public dDate As Date
Private Sub txtDate_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Worksheets("Sheet1").Columns(1).NumberFormat = "dd-mmm-yyyy"
If txtDate.Value = vbNullString Then
Exit Sub
ElseIf Not IsDate(txtDate.Value) Then
Cancel = True
MsgBox "Invalid date, please re-enter", vbCritical
txtDate.Value = vbNullString
txtDate.SetFocus
Worksheets("Sheet1").Range("A2").Value = Format(CDate(txtDate.Value), "dd-mmm-yyyy")
Exit Sub
End If
dDate = DateSerial(Year(Date), Month(Date), Day(Date))
txtDate.Value = Format(CDate(txtDate.Value), "dd-mmm-yyyy")
Worksheets("Sheet1").Range("A2").Value = Format(CDate(txtDate.Value), "dd-mmm-yyyy")
End Sub
Private Sub UserForm_Initialize()
Load userform1
userform1.Show vbModeless
With txtDate
.Value = Worksheets("Sheet1").Range("A2").Value
If .Value <> "" Then .Value = Format(.Value, "dd-mmm-yyyy")
End With
End Sub
Last edited: