Time comparison functio giving wrong results

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,570
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am having problems getting this code to return the results expected.

Code:
    For P = 38 To 47
        If ws_sbase.Range("B" & P) > CDate(dpgo) Then 'those that start before groom offset time, when the only program, is eligible
            ws_sbase.Range("B" & P & ":C" & P).ClearContents
            ws_sbase.Range("D" & P) = "X"
            ws_sbase.Range("E" & P) = ""
        End If
    Next P

Here is the databasethat this code is working with:

Excel 2010
ABCDE
37RP
38CRP7:00 AM3:00 PMX1
39CWP7:00 AM3:00 PMX2
40RPE7:00 AM3:00 PMB3
41RPL1:30 PM9:30 PMA4
42HPE7:00 AM3:00 PMB5
43WPE7:00 AM3:00 PMB6
44CUE7:00 AM3:00 PM57
45HPL1:30 PM9:30 PMA8
46WPL1:30 PM9:30 PMA9
47CUL4:00 PM12:00 AM610
SBASE


CDate(dpgo) in my testing = 7:00:00 AM

The code steps through each cell in range B38:B47 comparing the time in that cell with the time value of dpgo. If the time in cell B is greater than dpgo, that crew is excluded ad the cells B:D are cleared. All the others remain.

If this code was working, B41:D41, B45:D47 would be blanked out as the time value in B > dpgo. The others are = dpgo.

But in my testing, this code will blank the entire range.

Is anyone able to suggest why this is giving me the wrong results.
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Doesn't CDate convert it to a date? Do you have a date component on both elements for proper comparison?
 
Last edited by a moderator:
Upvote 0
It appears as though the decimal equivalents of ws_sbase.Range("B" & P) - 7:00 PM (.291666) and dpgo - also 7:00 PM (0.291663) are different. ws_sbase.Range("B" & P) is greater than dpgo when you consider their decimal eqivalents. I assume that is the problem.

How can I overcome this?
 
Upvote 0
Hi Delifinus25

Strip out the date formatting on the cells you are comparing and convert to number format with a few decimal places (ie "#,##0.0000") and then make the comparison. You'll see that 6am on 3April17 and 6am on 19Sep16 will both appear "6:00:00 AM" if that is how you have formatted it, but of course, 3April17 is greater than 19Sep16.

6AM 3April17 will appear 42828.25 as a number; 6AM 19Sep16 as 42632.25

The 0.25 is the "6AM" - a quarter of a whole number (6 / 24 hours).

Consequently you will have to make sure that you are using the required numbers 'behind' the times to get the right answer.

Have fun!

Cheers

pvr928
 
Upvote 0
Thank you all for your support.
I tracked down my problem. dpgo was a calculation of a time minus a decimal equivalent of 1 hour. (dpgo = prgm_start - 0.04167). This skewed the answer a bit. I changed the formula to dpgo = prgm_start - TimeSerial(1, 0, 0), and this resulted in the two values being equal. But I still wast able to get the comparison right. Once I rounded the two values to 5 decimal places, it finally worked.

Code:
                           For P = 38 To 47
                                ctime = ws_sbase.Range("B" & P)
                                dtime = dpgo
                                If Round(ctime, 5) > Round(dtime, 5) Then 'those that start before groom offset time, when the only program, is eligible
                                    ws_sbase.Range("B" & P & ":C" & P).ClearContents
                                    ws_sbase.Range("D" & P) = "X"
                                    ws_sbase.Range("E" & P) = ""
                                End If
                            Next P

Thanks for pointing me in the right direction.
 
Upvote 0
Doesn't CDate convert it to a date?

No. It returns a value of type Date. Values of type Date can include time, which is just a fraction of a day; or it can be time only (integer part is zero).

In some contexts, VBA treats type Date values in a special way that causes time to be rounded to the second. For that reason, I eschew the use of type Date, opting to use type Double instead.

CDate(dpgo) has dubious purpose in Ark68's context. We cannot be sure, since we do not know the type of dpgo. Presumably, it is type Variant or undeclared, which defaults to type Variant. In that case, the internal value of CDate(dpgo) is no different from dpgo by itself. Try the following example.
Code:
Sub doit()
Dim dbl As Variant
Dim dat As Date
dbl = 123.456789012345
dat = dbl
MsgBox dat & vbNewLine & _
   WorksheetFunction.Text(dbl, "m/d/yyyy h:mm:ss.000") & _
   vbNewLine & (dat > dbl)
End Sub
 
Upvote 0
Hi joeu2004, thanks for the lesson! I appreciate any opportunity to learn.
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top