Home > Software engineering >  VBA DateAdd and DateDiff function make the DATE from the h increased by the hour, why across the DAT
VBA DateAdd and DateDiff function make the DATE from the h increased by the hour, why across the DAT

Time:01-27

 
Sub test ()
Dim t, t1 As Date
Dim m, n As Integer
T="5-31-1999 00:00:00" : n=4: t1="6-30-1999 00:00:00"
For m=1 To DateDiff (" h ", t, t1)
Sheet1. Cells (n, 5)=t
N=n + 1
T=DateAdd (" h ", 1, t)
Next
End Sub


Date didn't increase while crossing zero, but zero after the next time is increasing, on the basis of an hour, has also increased the date one day, is how to change to date date more during cross zero day? Such as the production of the following forms?

1999/5/31 23:00:00

1999/6/1 00:00:00

1999/6/1 01:00:00

CodePudding user response:

Visual is office of bugs, so write can get the results you want
 
Sub test ()
Dim t As the Date, the t1 As Date
Dim m As a Double, n As Integer

T="5-31-1999 00:00:00" : n=4: t1="6-30-1999 00:00:00"
For m=t To t1 Step 1/24
Sheet1. Cells (n, 5)=m
N=n + 1
Next m
End Sub

CodePudding user response:

Later add
 Sheet1. [E4]. Resize (n - 4). NumberFormat="yyyy/mm/dd hh: mm: ss" 

CodePudding user response:

@ this life snow lion, brother was fantastic, perfect solution, wonder if 24 h system problem

CodePudding user response:

I also feel very strange, if I am not defined as type double, but the date type, also can appear the same bug

CodePudding user response:

After brother remind, modify the format just time format
 
Sub test ()
Dim t, t1 As Date
Dim m, n As Integer
T="5-31-1999 00:00:00" : n=4: t1="6-30-1999 00:00:00"
For m=1 To DateDiff (" h ", t, t1)
Sheet1. Cells (n, 5)=Format (t, "yyyy/mm/dd hh: mm: ss")
N=n + 1
T=DateAdd (" h ", 1, t)
Next
End Sub
  •  Tags:  
  • VBA
  • Related