View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
okaizawa okaizawa is offline
external usenet poster
 
Posts: 129
Default compare TIME in IF statement

Hi,
since time serial number is floating point number,
you should not compare time serial numbers simply.
I recommend to compare strings or integers.

Sub Test()

Range("A1").Value = TimeSerial(1, 40, 0)

'This shows False
MsgBox Range("A1").Value = TimeSerial(1, 40, 0)

'compare strings
MsgBox Format(Range("A1").Value, "hh:nn") = "01:40"

'compare total minute
MsgBox CLng(Range("A1").Value * (24 * 60)) = 1 * 60 + 40

'calculate the difference
If Abs(Range("A1").Value - TimeSerial(1, 40, 0)) < TimeSerial(0, 0,
1) Then
MsgBox "same"
End If

End Sub

--
hth
okaizawa

a wrote:
hi,

case 1
i have this column, with time in US locale and in the format H:MM , for
example, 9:30 in cell M2

My VBA code tries to compare

if range(M2).value = time(9,30,0) then DO SOMETHING

just fails to work..