View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Myrna Larson Myrna Larson is offline
external usenet poster
 
Posts: 863
Default error comparing dates

You are comparing a number (the result of CDate is a number between 0 and
0.99999999) with a text string and relying on VBA to properly interpret the
latter as a time and convert it to a time. IMO, that's risky business. I like
to be in control as much as possible, so I always convert the two variables to
the same type. IOW, if you convert one to a date, you should do the same with
the 2nd.

That said, in the immediate window I executed the following statement: the
result was False, not True

? cdate("10:00 AM") < "10:00:00 AM"
False

Note that

? cDate("10:00 AM") < CDate("10:00:00 AM")

also prints False

On Wed, 20 Oct 2004 20:01:04 -0700, "Fernando"
wrote:


Hi,
Error when comparing a range of dates, returns false instead of true.

code:
dim hrMax as date
Do While CDate(lstMax.List(lstMax.ListCount - 1)) < hrMax
....
loop

explanation:
lstMax (listbox control) has an array of of times ("11:00 AM", "12:00 AM",
"1:00 PM", etc), in the loop, the camparison goes like this:

Do While CDate("10:00 AM") < "10:00:00 AM" -- result: true
Do While CDate("11:00 AM") < "10:00:00 AM" -- result: false
Do While CDate("12:00 AM") < "10:00:00 AM" -- result: false
Do While CDate("1:00 PM") < "10:00:00 AM" -- result: false

why does the 1st returns true?.
when I debug step by step, I can see that the it should return false

This file manages data files, and this error is only present on very few

files

thanks for your help