Date validation for excel vba form

anderm13

New Member
Joined
Jan 22, 2010
Messages
3
Hi,

I'm trying to create validation for date field.
As it stands, the validation makes sure the date is in the correct format, but it allows you to enter past dates.

I need the date to be in the dd/mm/yyyy format and the date should be today or greater. No past dates.

Can anyone help?

This is the code i have so far:

If IsDate(startDate.Value) Then
If Format(startDate, vbLongDate) >= Format(Date, vbLongDate) Then
startDate = Format(startDate, "dd/mm/yyyy")
Else
MsgBox "The date you entered is in the past."
Cancel = True
End If
Else
MsgBox "The date entered is in an invalid format, please enter the date in DD/MM/YYYY format"
Cancel = True
End If
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
If Format(startDate, vbLongDate) >= Format(Date, vbLongDate) Then
The Format function turns a Date into a TEXT string.
so you can't really do a comparison for Greater Than with Text strings..
Just drop the Format functions..

If startDate >= Date Then
 
Upvote 0

Forum statistics

Threads
1,216,110
Messages
6,128,892
Members
449,477
Latest member
panjongshing

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