Date Validation

Damo10

Active Member
Joined
Dec 13, 2010
Messages
460
Hi,

I have a userform to enter a start date for my sheet, what I would like to do is to have a check to see if the date that has been entered is a valid date. If the date is not valid then a messagebox to state that the date is not valid.

Here is the code for the userform.

Code:
Private Sub Confirm_Click()
On Error Resume Next
 
If Not (Me.DateSel.Text Like "##/##/##") Then
 Cancel = True
 MsgBox "Invalid date entered, start date has not been changed", vbOKOnly, "Invalid Date"
 
 Exit Sub
 End If
 
Run "Unprotect"
Sheets("Holidays").Range("D18") = CDate(Me.DateSel.Value)
Run "Month"
Run "Protect"
Unload Me
End Sub

Regards
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Assuming this is a true user form and not a bunch of cells on the sheet made to look like a form, then why not use a DateTime picker, that way, the date has to be a valid date, so you may only want to validate that it's in the correct range.

The picker can be found under Additional controls… Microsoft Date and Time Picker Control 6.0 (SP6) (or similar).

Then you could do something like:
Code:
Private Sub Confirm_Click()
If Year(DTPicker1.Value) < 2011 Or Year(DTPicker1.Value) > 2013 Then
  Cancel = True
  MsgBox "Invalid date entered, start date has not been changed", vbOKOnly, "Date out of range"
  Exit Sub
End If
Run "Unprotect"
Sheets("Holidays").Range("D18") = DTPicker1.Value
Run "Month"
Run "Protect"
Unload Me
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,552
Messages
6,179,488
Members
452,917
Latest member
MrsMSalt

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