Time Comparisons Failing

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,570
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am having difficulty in getting the needed results from my code.

I have a variable dpgo, declared as DATE. This variable represents a time.
Code:
dpgo = prgm_start - TimeSerial(1, 0, 0)
In my testing, dpgo = 7:00:00 AM.

dpgo is then compared to the time values in a range of cells (B17:B26) of a database. If this value is less than or equal to dpgo (any times before or at 7:00AM in this example, it is considered valid and the data in the database remain. Any "invalid" data (where the time values in the range are greater than 7:00AM in this example) are eliminated. At the end of the loop, the database is reduced to show only those rows in which the time in column B is equal or less than 7:00AM (in the testing example). But when I run my code, all rows remain despite the value in column B.

Here is the code:
Code:
                         For P = 17 To 26
                            If tpgo = "<" Then
                                If ws_sbase.Cells(P, 2) >= CDbl(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
                            End If
                        Next P

When I step through the code, when I hover over ws_sbase.Cells(P, 2) I get a decimal value, I am assuming the time equivalent? When I hover over CDbl(dpgo) I get 7:00:00 AM.

Here's my database that is used in the comparison ...

Excel 2010
ABCDE
16WP
17CWP7:00 AM3:00 PM21
18WPE7:00 AM3:00 PMA2
19CUE11:00 AM7:00 PM43
20WPL1:30 PM9:30 PMC4
21CUL4:00 PM12:00 AM55
22CRP7:00 AM3:00 PM36
23HPE7:00 AM3:00 PMA7
24RPE7:00 AM3:00 PMA8
25HPL1:30 PM9:30 PMC9
26RPL1:30 PM9:30 PMC10
SBASE


Is anyone able to advise what may be wrong and what I can do to overcome this problem?
 
Last edited:

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
I'm afraid in my tests I'm getting the opposite of what you're seeing and it is erasing everything (which I would expect since all values in column B are greater than or equal to dpgo). Admittedly I had to make some tweaks to test on my own machine since I can't see the rest of your code. What I did was put the data you supplied in cells A1:E10, and then ran the following:

Code:
Sub Test()

Dim dpgo As Date
Dim P As Long
  
  'Restore Data
  [A17:E26].Value2 = [A1:E10].Value2
  dpgo = TimeValue("7:00 AM")
  
  For P = 17 To 26
    'If tpgo = "<" Then
    If ActiveSheet.Cells(P, 2) >= CDbl(dpgo) Then 'those that start before groom offset time, when the only program, is eligible
      ActiveSheet.Range("B" & P & ":C" & P).ClearContents
      ActiveSheet.Range("D" & P) = "X"
      ActiveSheet.Range("E" & P) = ""
    End If
    'End If
  Next P
  
End Sub

Also for me hovering over CDbl(dpgo) produces "0.291666666667". I'm rather surprised at what you say you're seeing when you hover over the same value.
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,315
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