none
Generate last 12 months using VBA RRS feed

  • Question

  • Hi,

    I have a button in Sheet1 and clicking on that button I want to populate last 12 months based on the system date. For example, if the system date is 04/13/2018 then starting from Cell A1 the code should print the months in the following order:

    March'18

    Febryary'18

    January'18

    December'17

    November'17

    October'17

    September'17

    August'17

    July'17

    June'17

    May'17

    April'17

    Any help in this regard would be appreciated.


    JPP

    Thursday, April 12, 2018 8:01 PM

Answers

  • Here is a macro you can assign to the button:

    Sub GenerateMonths()
        Dim i As Long
        Range("A1:A12").NumberFormat = "mmmm'yy"
        For i = 1 To 12
            Range("A" & i).Value = DateSerial(Year(Date), Month(Date) - i, 1)
        Next i
    End Sub


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Thursday, April 12, 2018 8:10 PM