How to compare 2 dates in VBA

J

Joy

I use

If FormatDateTime('3/1/2009', vbShortDate) < FormatDateTime("6/2008",
vbShortDate) = True Then
....


the result is that 3/1/2009 is before 6/2008, which is apparently wrong.

Thanks
 
J

Jack Dahlgren MVP

I would not compare the date strings as string comparison may give you
different results than expected. I'd compare the date value.
if dateValue("3/1/2009") ...

-Jack Dahlgren
 
J

Joy

Yes. I found I need to dim it as date instead of string.
Then, it works for me
Thank you!
 

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