Use VBA to subtract 3 years from a table of dates

jase71ds

Board Regular
Joined
Oct 23, 2006
Messages
137
Office Version
  1. 365
Platform
  1. Windows
I have tens of thousands of dates and I need to subtract 3 years from the dates.

The dates are in the American form of mm/dd/yyyy.

Let's say the range is, A1:Z100 (just to keep things simple)
Some of the cells are blank.
Some of the cells contain the text: "na" (no quotes)
But most of the cells have dates that need 3 years knocked off them. Thus 8-5-2018 would become 8-5-2015.

Any help is appreciated.
Thanks.
Jase.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
add an extra column and display it there, if you run code to remove three years and then run it again, your data is damaged beyond use
 
Upvote 0
Try

Code:
Sub test()
For Each c In Range("A1:Z100").SpecialCells(xlCellTypeConstants, 1)
    If IsDate(c) Then c.Value = DateAdd("yyyy", -3, c.Value)
Next c
End Sub
 
Upvote 0
Wow! Beautiful code. Works perfectly. Many thanks!!!!

Jase.
 
Upvote 0

Forum statistics

Threads
1,215,438
Messages
6,124,873
Members
449,192
Latest member
MoonDancer

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