Question : How do I use >= in VBA to compare dates

I am using VBA code to compare dates the line of code that I am using is:
If Format(PStart, "mm/dd/yy") >= Format("7/1/68", "mm/dd/yy") Then

PStart is a specific date field with dates like 12/6/76 or 7/11/69. This line is not properly recognizing dates. if PStart has a date of 12/4/65 or any other date before 7/1/68 it will continue though the if statement as if the if statement were true.

I have tried
If str(PStart) >= Format("07/01/68", "mm/dd/yy") Then
 But this produces the same result.

I would appreciate any help.

Thank you

Answer : How do I use >= in VBA to compare dates

Two things to look at:

First - make sure your variables are defined as DATE TIME - not string. Otherwise the string

01/01/00  will always be less than 01/01/99 because the year 2000 won't be evaluated correctly.

If you MUST use a string then try:

If Format(PStart, "YY/MM/DD") >= Format("7/1/68", "YY/MM/DD") Then ...

Again - this forces the year hierarchy so your numbers can be evaluated correctly.

Random Solutions  
 
programming4us programming4us