I want to replace a repeating year of dates in a column.
I started with a nested for loop in a single module, then I changed the code to be used for each sheet in it's relative module.
I tried replacing the repeating year with nothing and then adding the right year but it hangs at the middle.
Sub change_dates()
Dim wb As Workbook
Dim o As Long
Dim k As Long
Dim y As Long
Set wb = ThisWorkbook
y = Year(Date)
o = 2
k = wb.Worksheets(11).Cells(2, 10).Value + 2
Do While o < k
If Mid(wb.Worksheets(11).Cells(o, 1), 4, 2) = 12 Then
y = y - 1
End If
wb.Worksheets(11).Cells(o, 1) = Left(wb.Worksheets(11).Cells(o, 1), 6) & CStr(y)
o = o + 1
Loop
End Sub
The cell values after I run the code:
...
14/03/2014
14/02/2014
14/01/2014
13/12/2013
13/11/2013
13/10/2013
13/09/2013
13/08/2013
13/07/2013
13/06/2013
13/05/2013
13/04/2013
13/03/2013
13/02/2013
13/01/2013
12-12-2012
11-12-2012
10-12-2012
09-12-2012
08-12-2012
07-12-2012
06-12-2012
05-12-2012
04-12-2012
03-12-2012
02-12-2012
01-12-2012
12-11-2011
11-11-2011
...
The first part is how I want it. In the second part the month is changing and the formatting too.
The original values of the column.
... 22-01-2022 21-12-2022 21-11-2022 21-10-2022 21-09-2022 21-08-2022 21-07-2022 21-06-2022 21-05-2022 21-04-2022 21-03-2022 21-02-2022 21-01-2022 20-12-2022 20-11-2022 20-10-2022 20-09-2022 20-08-2022 20-07-2022 20-06-2022 ...
This is the desired result:
22-01-2022 21-12-2021 21-11-2021 21-10-2021 21-09-2021 21-08-2021 21-07-2021 21-06-2021 21-05-2021 21-04-2021 21-03-2021 21-02-2021 21-01-2021 20-12-2020 20-11-2020 20-10-2020 20-09-2020 20-08-2020 20-07-2020 ...
Further clarifications:
- I get date as a result of =A2+1.
- The A column cells are all formatted dd-mm-yyyy
- k is returning the right number.
- The result should be, all the days and months remaining the same, and the repeating year 2022 changed to a year that starts in 2022 and decreases by one starting from the top down every time it's December.