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

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,961
Office Version
  1. 365
Platform
  1. Windows
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

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
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

Iceberg

Board Regular
Joined
Mar 12, 2008
Messages
79
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

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,961
Office Version
  1. 365
Platform
  1. Windows
Code:
Sub test()
Do Until ActiveCell > Date
    ActiveCell = DateAdd("yyyy", 5, ActiveCell)
Loop
End Sub
 
Upvote 0

draggar

New Member
Joined
Jun 28, 2008
Messages
1
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,190,770
Messages
5,982,830
Members
439,799
Latest member
matts12

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
Top