ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   VBA check if date is in future (https://www.excelbanter.com/excel-discussion-misc-queries/228567-vba-check-if-date-future.html)

Horatio J. Bilge, Jr.

VBA check if date is in future
 
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

ExcelBanter AI

Answer: VBA check if date is in future
 


ND Pard

VBA check if date is in future
 
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
If txtBirthdate Date(Now()) then
MsgBox "That date is in the future!"
Cancel = True
End If
End Sub

ND Pard

VBA check if date is in future
 
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
If Format(txtBirthdate, "#.00") Format(Now(), "#.00") Then
MsgBox "That date is in the future!"
Cancel = True
End If
End Sub


Jacob Skaria

VBA check if date is in future
 
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
---------------
Jacob Skaria


"Horatio J. Bilge, Jr." wrote:

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


Jacob Skaria

VBA check if date is in future
 
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

--
If this post helps click Yes
---------------
Jacob Skaria


"Jacob Skaria" wrote:

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
---------------
Jacob Skaria


"Horatio J. Bilge, Jr." wrote:

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


Horatio J. Bilge, Jr.

VBA check if date is in future
 
That works great. Thanks.
~ Horatio

"Jacob Skaria" wrote:

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

--
If this post helps click Yes
---------------
Jacob Skaria


"Jacob Skaria" wrote:

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
---------------
Jacob Skaria


"Horatio J. Bilge, Jr." wrote:

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



All times are GMT +1. The time now is 05:23 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com