Sub 巨集1()
Dim a1 As Date
Dim a2 As Date
'測試1
a1 = "07:00:00"
a2 = "07:30:00"
a1 = DateAdd("n", 30, a1) 'a1 = 07:30:00 am
Debug.Print (a1 = a2) '結果示:True
'測試2
a1 = "06:30:00"
a2 = "07:30:00"
a1 = DateAdd("n", 30, a1) 'a1 = 07:00:00 am
a1 = DateAdd("n", 30, a1) 'a1 = 07:30:00 am
Debug.Print (a1 = a2) '結果顯示:False
End Sub
請教各位高手,為何在測式2的結果不如預期?謝謝。
因為你做了這動作兩次a1 = DateAdd("n", 30, a1)
如果你只做一次,像這樣a1 = DateAdd("n", 60, a1)
(加兩次30分鐘,換成加一次60分鐘)
答案就會是 true 了
至於為什麼會這樣
我也不知道
Sub 巨集1()
Dim a1 As Date
Dim a2 As Date
'測試2
a1 = "06:30:00"
a2 = "07:30:00"
a1 = DateAdd("n", 30, a1) 'a1 = 07:00:00 am
a1 = DateAdd("n", 30, a1) 'a1 = 07:30:00 am
Debug.Print CDbl(a2) - CDbl(a1)
End Sub
兩者差了 5.55111512312578E-17