1

How do we compare two date strings in vba, like "01.02.2013 < 02/02/2013"? Whatever the dates are this always showing true. And two date formats are correct in the example i menioned.

Below vba code throws error.

Sub aa()
Dim a As Variant, b As Variant, c As Variant
a = Format("1.2.2012", "DD\/MM\/YYYY")
b = Format("2.2.2012", "DD\/MM\/YYYY")
MsgBox (a)
End Sub
1
  • 1
    Those aren't dates, they are date strings. Convert them to the Date datatype first, then compare them. Commented Jun 3, 2013 at 14:56

2 Answers 2

1

Convert your dates (strings) to a format CDate() accepts. 02/02/2013 works, I think 02.02.2013 doesn't. Use Replace() if needed.

Then you can cast your String data into the Date datatype with CDate(myString). Dates can be compared with each other by the means of the usual operators, such as > < =.

Sign up to request clarification or add additional context in comments.

Comments

1

Clean up the strings to the correct format (see what CDATE accepts, and then use CDate(a) >= CDate(b) to compare.

1 Comment

apologies, Aeronth - hadn't seen your answer, mine is the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.