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 + 1, Month, Day)
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
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 + 1, Month, Day)
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