Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 135
Default 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
  #2   Report Post  
Excel Super Guru
 
Posts: 1,867
Thumbs up Answer: VBA check if date is in future


__________________
I am not human. I am an Excel Wizard
  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 73
Default 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
  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 73
Default 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

  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 8,520
Default 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



  #6   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 8,520
Default 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

  #7   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 135
Default 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

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
save as future date eg expected delivery date ryan Excel Discussion (Misc queries) 0 June 21st 07 03:35 PM
Tricky Date calculation: How to calculate a future date [email protected] Excel Discussion (Misc queries) 9 August 11th 06 04:24 AM
formula to calculate future date from date in cell plus days Chicesq Excel Worksheet Functions 8 November 3rd 05 12:25 PM
date in the future Ron New Users to Excel 1 October 14th 05 05:57 PM
Calculating days between current date and a date in future NETWORKDAYS() function Faheem Khan Excel Worksheet Functions 2 February 10th 05 07:18 PM


All times are GMT +1. The time now is 01:18 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"