简体   繁体   中英

Add month to previous cells date For each loop

All,

I have written a little procedure which I would like a for each loop to insert the current month into the first cell "01"/MM/YY and then add one month to the date as it goes through the loop. Using the example below;

K1 = 01/06/2018
L1 = 01/07/2018
M1 = 01/08/2018 etc

The code I am using is below - The error is on the DateAdd line.

Sub test()
Dim dt As date
dt = "01/" & Application.Text(Now(), "MM/YY")
Dim i As Double
i = 1

For Each c In Range("K1:XFD1")
    If c.Value = "" Then Exit For  
 c.Value = dt
'change date to one months time
 dt = DateAdd(m, i, dt)
 i = i + 1     
Next c

End Sub

Any help regarding this would be much appreciated.

Put "m" not m . m would be a variable. "m" is a literal string representing the argument being "month".

dt = DateAdd("m", i, dt)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM