Hi
I'm hoping someone can help with this problem. I have a workbook that uses a userform to populate various cells. One of the cells uses a textbox to populate a date (I initially used a monthview datepicker to select this but an update has removed the options so I've changed it to a textbox. The code I've used confirms that the input is a date, then populates the relevant cell using CDate, but if someone enters an impossible date, such as 32 Dec 2019, it populates the cell as 19 Dec 2032. I need it to return an error if the date isn't correct, is there anyway to do this? The code is pasted below:
Private Sub TextBox1_afterupdate()
If IsDate(TextBox1) Then
TextBox1.Value = Format(TextBox1.Value, "DD-MMM-YY")
Sheets("sheet1").Range("i2") = CDate(TextBox1)
End If
End Sub
Once this has processed it runs through certain validations below, so ideally I'd like ElseIf not IsDate to pick up that it isn't a valid date.:
ElseIf TextBox1 = "" Then
MsgBox "You must enter a Start date", _
vbOKOnly + vbCritical, "Entry Error"
TextBox1.SetFocus
Exit Sub
ElseIf Not IsDate(TextBox1) Then
TextBox1.Value = ""
MsgBox "Please enter a valid date", vbOKOnly + vbCritical, "Entry Error"
TextBox1.SetFocus
Exit Sub
ElseIf Range("i2") < Date Then
TextBox1.Value = ""
MsgBox "The start date you've entered is in the past", _
vbOKOnly + vbCritical, "Entry Error"
TextBox1.SetFocus
Exit Sub
ElseIf Range("i2") > Range("d3") Then
TextBox1.Value = ""
MsgBox "The start date you've entered is more than a year in advance", _
vbOKOnly + vbCritical, "Entry Error"
TextBox1.SetFocus
Exit Sub
thanks
Bookmarks