Excel VBA - Pushing dates in multiple columns forward by one year based upon date in specific column

Simple77

New Member
Joined
Dec 6, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi, long time reader first time post!!
I am still at novice level in VBA but keen to learn more. Through various resources, I have been able to construct some coding - see below - but am having trouble with getting the desired end results.
I have four columns of dates (Col's a to D), where I am trying to get the coding to move dates forward by one year, but only when the date in the last column (Coll D) is a past date. i.e. less than today. Whenever I run it, all dates in the stated columns are being updated bu a year, even future dated ones, so suspect the issue lies where I have attempted to identify the driving column. Any assistance will be greatly appreciated.

Sub AAGGGHHH()
Dim n As Long 'Last row
Dim r As Range, h As Range, i As Range, j As Range, k As Range
Dim rng As Range 'Column D being the lead
Dim hrng As Range 'Column A
Dim irng As Range 'Column B
Dim jrng As Range 'Column C

Dim d As Date 'Date formation
Dim w As Date, x As Date, y As Date, z As Date

n = Cells(Rows.Count, "A").End(xlUp).Row 'Defines last row
Set rng = Range("D3:D" & n) 'Defines range to look at
Set hrng = Range("A3:A" & n)
Set irng = Range("B3:B" & n)
Set jrng = Range("C3:C" & n)

For Each r In rng 'For each cell in Column D
d = r.Value 'Confirms date is populated in each cell in Column D
If d < Date Then 'If date in each cell is less than Today()
For Each h In hrng
w = h.Value
h.Offset(0, 0).Value = DateSerial(Year(w) + 1, Month(w), Day(w))
Next h
For Each i In irng
x = i.Value
i.Offset(0, 0).Value = DateSerial(Year(x) + 1, Month(x), Day(x))
Next i
For Each j In jrng
y = j.Value
j.Offset(0, 0).Value = DateSerial(Year(y) + 1, Month(y), Day(y))
Next j
For Each k In rng
z = k.Value
k.Offset(0, 0).Value = DateSerial(Year(z) + 1, Month(z), Day(z))
Next k
End If
Next r
End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Addup same date, same month, 1 year yields special case: if D is leaf year: 29-Feb-2020, what date to return next year?
using DateSerial function, it returns 1-Mar-2021
I am using edate function, which give 28-Feb-2021 from D (last day of feb)
VBA Code:
Sub test()
Lr = Cells(Rows.Count, "D").End(xlUp).Row
For i = 3 To Lr
    Debug.Print Cells(i, "A").Value
    If Cells(i, "D") < Date Then
    Cells(i, "A").Value = Application.EDate(Cells(i, "A"), 12)
    Cells(i, "B").Value = Application.EDate(Cells(i, "B"), 12)
    Cells(i, "C").Value = Application.EDate(Cells(i, "C"), 12)
    End If
Next
End Sub
 
Upvote 0
Addup same date, same month, 1 year yields special case: if D is leaf year: 29-Feb-2020, what date to return next year?
using DateSerial function, it returns 1-Mar-2021
I am using edate function, which give 28-Feb-2021 from D (last day of feb)
VBA Code:
Sub test()
Lr = Cells(Rows.Count, "D").End(xlUp).Row
For i = 3 To Lr
    Debug.Print Cells(i, "A").Value
    If Cells(i, "D") < Date Then
    Cells(i, "A").Value = Application.EDate(Cells(i, "A"), 12)
    Cells(i, "B").Value = Application.EDate(Cells(i, "B"), 12)
    Cells(i, "C").Value = Application.EDate(Cells(i, "C"), 12)
    End If
Next
End Sub
Thank you so much. How you condensed the individual rules I had tried into one routine is great. It works perfectly. Concerning your point around leap year, that should not be an issue, as the backend process of compiling the dates will ignore 29th Feb, and just stick to 28th Feb and 1st Mar. Once again, Many Thanks.
 
Upvote 0

Forum statistics

Threads
1,215,742
Messages
6,126,611
Members
449,321
Latest member
syzer

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