I would like the ability for the user to enter a year (eg "2013") in cell D1 and press a button that fires off a macro. This macro will automatically assign a function to cells D2-O2 (one cell for each month in the year) that converts these cells to actual date types.
For instance, cell D2's value would be =DATE(2013, 1, 1)
, signifying that this cell represents January 1st of 2013. Similarly, cell E2's value would be =DATE(2013, 2, 1)
, F2's value would be =DATE(2013, 3, 1)
, etc.
The following is my pseudo code, could you please help me convert this to actual VBA?
var myYear = the value of cell D1
cell D2 value is =DATE(2013,1,1)
cell E2 value is =DATE(2013,2,1)
cell F2 value is =DATE(2013,3,1)
cell G2 value is =DATE(2013,4,1)
cell H2 value is =DATE(2013,5,1)
cell I2 value is =DATE(2013,6,1)
cell J2 value is =DATE(2013,7,1)
cell K2 value is =DATE(2013,8,1)
cell L2 value is =DATE(2013,9,1)
cell M2 value is =DATE(2013,10,1)
cell N2 value is =DATE(2013,11,1)
cell O2 value is =DATE(2013,12,1)
Thanks
Try this:
Sub FillYear()
YearNum = Cells(1, 4) ' Cell D1
For MonthNum = 1 To 12
Cells(2, MonthNum + 3).Value = DateSerial(YearNum, MonthNum, 1)
Next
End Sub
UPDATE:
If you want the date values to be a formula, so that the dates change if the user changes the year, change the line inside the For Loop
to be:
Cells(2, MonthNum + 3).Formula = "=Date(R1C4," & MonthNum & ", 1)"
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.