To create a macro in Excel that adds 14 days to the date in cell J2 when a button is pressed, you can use the following VBA code:
- Open Excel and press
ALT + F11 to open the Visual Basic for Applications (VBA) editor.
- In the VBA editor, insert a new module by right-clicking on any of the items in the Project Explorer and selecting
Insert > Module.
- Copy and paste the following code into the module:
Sub AddDays()
Dim currentDate As Date
Dim newDate As Date
' Get the date from cell J2
currentDate = Range("J2").Value
' Add 14 days
newDate = DateAdd("d", 14, currentDate)
' Output the new date back to cell J2
Range("J2").Value = newDate
End Sub
- Close the VBA editor and return to your Excel sheet.
- To create a button, go to the
Developer tab, click on Insert, and choose a Button (Form Control). Draw the button on your sheet.
- When prompted to assign a macro, select
AddDays and click OK.
- Now, when you click the button, it will add 14 days to the date in cell J2.
This macro uses the DateAdd function to add days to a specified date, which is a common approach in VBA for manipulating dates.
References: