Date less than Today

Noz2k

Well-known Member
Joined
Mar 15, 2011
Messages
693
I need to specify that the date value entered in a textbox cannot be greater than todays date, and was wondering if anybody could help?
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Maybe something along these lines?

Code:
If Textbox1.Value > Date Then
    MsgBox "Please enter a date earlier than today: " & Date
    Exit Sub
Else
    'Your Code
End If
 
Upvote 0
Sorry, I should have explained better.

I have tried using that, but the date value entered is in the format dd/mm/yyyy which I think is what is preventing this from working.
 
Upvote 0
Try:

Code:
If Textbox1.Value > Format(Date, "dd/mm/yyyy") Then
    MsgBox "Please enter a date earlier than today: " & Date
    Exit Sub
Else
    'Your Code
End If
 
Upvote 0
No sorry, my mistake, I only changed the year when checking.

I think the problem I have is that the value entered into the textbox isn't being recognised as a date.

So in essence it is reading the date as the number 21032011.

It's seeing 22/03/2010 as greater than 21/03/2011.


Not to worry I corrected the code myself using CDate.

Code is now

Code:
If CDate(Textbox.Value) > Format(Date, "dd/mm/yyyy") Then
    MsgBox "Date cannot be in the future. Today is " & Date
 
Last edited:
Upvote 0
Then we need to convert that number into a date by using DateSerial and some string functions to strip out the day, month, year.

Code:
If DateSerial(Right$(Textbox1.Value, 4), Mid$(Textbox1.Value, 3, 2), Left$(Textbox1.Value, 2)) > Date Then
    MsgBox "Please enter a date earlier than today: " & Date
    Exit Sub
Else
    'Your Code
End If
 
Upvote 0
It's ok, I just used CDate which seems to have done the trick.

Thanks very much for your help though
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,874
Members
452,949
Latest member
Dupuhini

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