I'm trying to create a loop to increment the date (Month/Day/Year Hour:Minute) by 14 days in specified cells.
Cell A2 is the start date, Cell A3 is the incremented date by 14, and Cell B3 is the stop date.
Sheet | |
---|---|
3/1/2020 0:00 | |
3/14/2020 11:59 | 12/31/2020 11:59 |
I have used the following code to increment Cells A2 and A3 by 1 day, but I can't figure out how to increment the days by 14.
The final date the loop would stop at is the date set in cell B3.
Also, when the code seems to start the date at 1/1/1900 instead of the date specified in cell A2 for some reason. Is there a way I can start the loop at the specified date in cell A2?
Sub test()
Start = Val(Range("A1").Value)
For Row = Val(Range("A3").Value) To Val(Range("B3").Value)
Range("A" & Row).Value = Start Row
Next
End Sub
Thanks!
CodePudding user response:
You can use Dateadd
to do this.
Dim mydate As Date
Dim row As Long
With ActiveSheet
mydate = DateAdd("d", 14, .Cells(2, 1).Value) 'Get first increment
row = 3 'First row to place values in
Do Until mydate >= .Cells(3, 2).Value
.Cells(row, 1).Value = mydate
row = row 1
mydate = DateAdd("d", 14, mydate)
Loop
End With
CodePudding user response:
Please, try the next adapted code:
Sub testIncrement14Days()
Dim start As Date, row As Long
start = Range("A1").value: row = 3
Do While start <= CDate(Range("C3").value) 14
Range("A" & row).value = start 14
start = CDate(Range("A" & row).value)
row = row 1
Loop
End Sub