View Single Post
  #9   Report Post  
Posted to microsoft.public.excel.programming
Jim Rech Jim Rech is offline
external usenet poster
 
Posts: 2,718
Default VBA Time comparison not correct

Well Text isn't so great for greater then/ less than comparisons.

Back to Value, I wonder if rounding will work:

Round(Cells(n, 1).Value,10) = Round(Cells(1,8).Value,10)

"10" is arbitrary of course.

--
Jim
"wright" wrote in message
...
| This worked for my example but unfortunately creates a new issue with my
| actual code.
|
| I am actually (and one of the reasons for the loop) entering 2 times and
| want to put a 1 next to all times = the first time and <= the last time.
|
| Dim n As Integer
| For n = 1 To 1440
| 'check if column 1 value is later than H1
| If Cells(n, 1) = Cells(1, 8) Then
| 'check if column 1 value is before I1
| If Cells(n, 1) <= Cells(1, 9) Then
| Cells(n, 2) = 1
| End If
| End If
| Next n
|
| Now with comparing the text values:
|
| Dim n As Integer
| For n = 1 To 1440
| 'check if column 1 value is later than H1
| If Cells(n, 1).Text = Cells(1, 8).Text Then
| 'check if column 1 value is before I1
| If Cells(n, 1).Text <= Cells(1, 9).Text Then
|
| Cells(n, 2) = 1
| End If
| End If
| Next n
|
| and using h1 = 15:00 and I1=23:59
|
| I get 1:00 to 1:59 and 15:00 to 23:59 marked.
|
| (note: I am also doing other calculations in this formula which require
the
| double if statements and the Cells(r,c) formats)