none
Comparison between Dates - greater or equal than and less or equal than RRS feed

  • Question

  • Hi Everyone,

    I have come across a weird thing that I can't seem to wrap my head around. What I want to do is to compare whether a date d1 is greater than or equal to a certain date d2 and less than or equal to another date d3 in a VBA program. For this, I would like to make use of the representation of a date as a number.

    However, when I set all dates equal (i.e., d1=d2=d3), the following code expression evaluates to false:

    d1>=d2 and d1<=d3

    To rule out all errors I could think of, I have even extended the comparison to

    cdate(d1)>=cdate(d2) and cdate(d1)<=cdate(d3)

    Most astonishingly,

    cdate(d2)=cdate(d3)

    as well as  

    d2=d3 

    both return "true".

    What am I not getting here?

    Thanks a lot for any help,

    Dormen

    • Moved by Reed KimbleMVP Tuesday, January 28, 2014 8:34 PM from vb language
    Tuesday, January 28, 2014 2:51 PM

All replies

  • We need to have a moderator move your question to the VBA forum. This forum is primarily for Visual Basic .NET questions.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Proposed as answer by Cor Ligthert Tuesday, January 28, 2014 5:25 PM
    Tuesday, January 28, 2014 4:36 PM
  • We need to have a moderator move your question to the VBA forum. This forum is primarily for Visual Basic .NET questions.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Done.  Forgot to unpropose this though and now don't have permission =P

    Dormen, you may want to unpropose the above post.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, January 28, 2014 8:36 PM
  • Oh, having read this now I would say that you should always use parentheses around expressions when combining them with Boolean operators to ensure you get the desired results... the statement may not be executed in the expected manner without the parentheses.

    Consider trying: (d1>=d2) and (d1<=d3)

     

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, January 28, 2014 8:43 PM