(Solved) – VBA to Extract years between Two dates and add Custom Label


the below VBA code is extracting the Month and Year, is there possible to get only the year and custom string in the adjacent column as Year1, Year2…

Expected results
Year Label

2020 Year1
2021 Year2

`Sub ExtractYears()
 Dim sc As Range
 Dim Stdt As Date
 Dim Edt As Date
 Dim dDate As Date
 Dim off As Integer
 Stdt = Range("B1") ' start date
 Edt = Range("B2") ' end date
 Set sc = Range("c2") ' start cell
' To clear the existing contents in Range D2 till end
 Range(Selection, Selection.End(xlDown)).Select

 off = 0
 For dDate = Stdt To Edt
    If Format(dDate, "dd") <= "01" Then
      sc.Offset(off, 0) = Format(dDate, "mmmm yyyy")
      off = off   1
    End If
 Next dDate

sc.Resize(off, 1).NumberFormat = "mmmm yyyy"
End Sub

Thank you for your time and suggestions

Leave a Reply

Your email address will not be published. Required fields are marked *