Consulting

Results 1 to 9 of 9

Thread: Date generating

  1. #1
    VBAX Newbie
    Joined
    Apr 2017
    Posts
    3
    Location

    Date generating

    Hi, I'm trying to find a way to generate a list of monthly dates between two specified dates, whilst taking into account that some months have 28/29/30/31 days. Cannot for the life of me work out how!

  2. #2
    VBAX Expert
    Joined
    May 2016
    Posts
    604
    Location
    you can do it with the following formula: the start date is in A1, put this formula into A2 then copy the formula down column A

    =DATE(YEAR(A1),MONTH(A1)+1,DAY(A1))

  3. #3
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    613
    Location
    .
    How about ... enter START date in A1.

    Click A1

    Left click and hold the drag handle / lower right corner and drag down as far as you need to.

    Unless of course you are attempting to do something completely different ? More info please.

  4. #4
    VBAX Newbie
    Joined
    Apr 2017
    Posts
    3
    Location
    Sorry, I'd like to do it via a command button and vba code if possible.

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Sub Test()
        Dim Dts, Strt, Endd, i
        Dts = InputBox("Start date, Months  e.g. 05/01/2017,6", , "05/01/2017,6")
        Strt = DateValue(Split(Dts, ",")(0)): Endd = Split(Dts, ",")(1)
        With ActiveCell
            .Resize(Endd + 1).NumberFormat = ("dd/mm/yy")
            .Value = DateValue(Strt)
            For i = 1 To Endd
                .Offset(i) = WorksheetFunction.EoMonth(Strt, i-1) + Day(Strt)
            Next i
        End With
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    613
    Location
    mdmackillop

    Nice code !

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    @ Logit
    Thanks
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    VBAX Newbie
    Joined
    Apr 2017
    Posts
    3
    Location
    Indeed a Nice bit of code! Unfortunately it doesn't take into account whether there are 28/29/30/31 days in the month.

    Ideally I like to generate the following:
    Start date 31/01/17
    31/01/17
    28(29)/02/17
    31/03/17
    30/04/17
    Etc

  9. #9
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    That was not clear from your question.
    Sub Test()    Dim Dts, Strt, Endd, i
        Dts = InputBox("Start date, Months  e.g. 05/01/2017,6", , "05/01/2017,6")
        Strt = DateValue(Split(Dts, ",")(0)): Endd = Split(Dts, ",")(1)
        With ActiveCell
            .Resize(Endd).NumberFormat = ("dd/mm/yy")
            .Value = DateValue(Strt)
            For i = 0 To Endd - 1
                .Offset(i) = WorksheetFunction.EoMonth(Strt, i)
            Next i
        End With
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •