Adding exactly 1 year to a date in VBA

Iceberg

Board Regular
Joined
Mar 12, 2008
Messages
79
Hello,

I have a spreadsheet with a column of dates and a command button. When I press the command button, I would like the macro to look at the dates and add exactly 1 year to each of them. How would I go about doing this?

Thanks!
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Perhaps:

Code:
Private Sub CommandButton1_Click()
Dim c As Range
For Each c In Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
    If IsDate(c) Then c = DateAdd("yyyy", 1, c)
Next
End Sub
 
Upvote 0
This assumes:
Your dates are in column A
You have a header in row 1, If Not, change
for i = 2 to LR
to
for i = 1 to LR

Code:
Private Sub CommandButton1_Click()
Dim LR As Long
Dim x As Date
LR = Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To LR
        x = Cells(i, "A").Value
        Cells(i, "A").Value = DateSerial(Year(x) + 1, Month(x), Day(x))
Next i
End Sub

Hope this helps...
 
Upvote 0
Follow-up question...I would like the macro to look at a date and add increments of 5 years until the date is past today's date.

Here's the general idea:

Rich (BB code):
X = ActiveCell.Value
Do Until ActiveCell.Value > (TodaysDate)
  ActiveCell.Value = DateSerial(Year(x) + 5, Month(x), Day(x))
Loop

Any suggestions?
 
Upvote 0
Code:
Sub test()
Do Until ActiveCell > Date
    ActiveCell = DateAdd("yyyy", 5, ActiveCell)
Loop
End Sub
 
Upvote 0
I registered to ask the same question. Interesting that Google didn't pick this one up but the code that HotPepper supplied (with some modification) is exactly what I was looking for.

(Thank you!)*10
 
Upvote 0

Forum statistics

Threads
1,214,593
Messages
6,120,435
Members
448,962
Latest member
Fenes

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top