VBA check if date is in future

  • Thread starter Horatio J. Bilge, Jr.
  • Start date
H

Horatio J. Bilge, Jr.

I have a userform where the user will input their birthdate. I used the
IsDate function to make sure they enter a valid date. I would like to add
code to catch dates that are in the future (e.g., "If txtBirthdate.Value >
Today Then...").

This is the code I have so far:
Private Sub txtBirthdate_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If txtBirthdate = vbNullString Then Exit Sub
If Not IsDate(txtBirthdate) Then
MsgBox "That is not a valid birthdate."
Cancel = True
End If
End Sub
 
J

Jacob Skaria

Hi

If you are using ISDATE() then you dont need to validate Nullstring. Also
use CDATE() to typecast the variable to date and compare the date with
current date using DATEDIFF() function as below..

If Not IsDate(txtBirthDate) Then
MsgBox "That is not a valid birthdate." : Cancel = True
Else
If DateDiff("d", CDate(txtBirthDate), Date) < 0 Then
MsgBox "That is a future date" : Cancel = True
End If
End If



If this post helps click Yes
 
J

Jacob Skaria

Check using greater than...

If Not IsDate(txtBirthDate) Then
MsgBox "That is not a valid birthdate.": Cancel = True
Else
If CDate(txtBirthDate) > Date Then
MsgBox "That is a future date": Cancel = True
End If
End If
 
H

Horatio J. Bilge, Jr.

That works great. Thanks.
~ Horatio

Jacob Skaria said:
Check using greater than...

If Not IsDate(txtBirthDate) Then
MsgBox "That is not a valid birthdate.": Cancel = True
Else
If CDate(txtBirthDate) > Date Then
MsgBox "That is a future date": Cancel = True
End If
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top