+ Reply to Thread
Results 1 to 5 of 5

Generate Dates by Month along columns based on Start Date and End Date

Hybrid View

Stndsh Generate Dates by Month along... 09-21-2015, 10:52 AM
MickG Re: Generate Dates by Month... 09-21-2015, 11:23 AM
jdawson Re: Generate Dates by Month... 09-21-2015, 11:31 AM
Stndsh Re: Generate Dates by Month... 09-22-2015, 09:27 PM
MickG Re: Generate Dates by Month... 09-23-2015, 05:32 AM
  1. #1
    Registered User
    Join Date
    08-18-2011
    Location
    New York, NY
    MS-Off Ver
    Excel 2003
    Posts
    12

    Generate Dates by Month along columns based on Start Date and End Date

    Hello,

    I found the attached VBA for generating dates but can't get it to work. Below is what I am trying to achieve:

    I would like to enter a start date and end date on the first sheet of the attached. I have created names for start date and end date and in the attached file. On the second sheet I would like the start date to populate in A1 and then the next month to populate in B1, the month after to populate in C1, etc. all the way to the end date (which is defined on the first sheet).

    Could someone help me with the VBA? Sheet is attached and VBA below:

    Sub GenerateDatesH()

    Dim FirstDate As Date
    Dim LastDate As Date
    Dim NextDate As Date
    Dim DateOffset As Range
    Dim DateIter As Date

    FirstDate = Range("startdate").Value
    LastDate = Range("enddate").Value
    Set DateOffset = Range("C1")

    For DateIter = FirstDate To LastDate
    DateOffset.Value = DateIter
    Set DateOffset = DateOffset.Offset(0, 1)
    Next DateIter

    End Sub

    Thank you!
    Attached Files Attached Files

  2. #2
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Generate Dates by Month along columns based on Start Date and End Date

    Try this:-
    Private Sub CommandButton1_Click()
    Dim StDt As Date, EnDt As Date, n As Long, c As Long
    StDt = Sheets("Project Info").Range("B4")
    EnDt = Sheets("Project Info").Range("B5")
    StDt = DateAdd("m", -1, StDt)
    Do Until DateAdd("m", c, StDt) = EnDt
        c = c + 1
        Sheets("Cash Flow").Cells(1, c) = DateAdd("m", c, StDt)
    Loop

  3. #3
    Registered User
    Join Date
    03-06-2006
    Location
    South Central Pennsylvania
    MS-Off Ver
    MS Office Pro Plus 2016, on Window 10
    Posts
    27

    Re: Generate Dates by Month along columns based on Start Date and End Date

    Hi. I tried this with a workbook with two worksheets (Input, Output). Sorry, it iterates by one day. The previous reply is better.
    Your named ranges are on Input and formatted as dates like 9/21/2015 and the entire first row on Output is similarly formatted.
    Option Explicit
    
    Sub GenerateDatesH()
    
     Dim FirstDate As Date
     Dim LastDate As Date
     'Dim NextDate As Date 'Not used
     Dim DateOffset As Range
     Dim DateIter As Date
     Dim i As Long
     Dim wsIn As Worksheet
     Dim wsOut As Worksheet
     
     Set wsIn = Worksheets("Input") 'Change as needed ("Input" is the name of the worksheet)
     Set wsOut = Worksheets("Output") 'Change as needed (Assumes that the date format has been defined for all cells in row 1.)
    
     FirstDate = wsIn.Range("startdate").Value
     LastDate = wsIn.Range("enddate").Value
     Set DateOffset = wsOut.Range("C1")
    
     i = 0
     For DateIter = FirstDate To LastDate
        DateOffset.Offset(0, i) = DateIter
        i = i + 1
     Next DateIter
    
     End Sub
    Last edited by jdawson; 09-21-2015 at 11:52 AM. Reason: Did not read carefully and assumed date increment as one day.

  4. #4
    Registered User
    Join Date
    08-18-2011
    Location
    New York, NY
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Generate Dates by Month along columns based on Start Date and End Date

    Thank you both for your responses! MikeG, you are amazing. But just a note for anyone that stumbles upon this post, if you copy and paste MikeG's VBA, don't forget to type End Sub at the end!

  5. #5
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Generate Dates by Month along columns based on Start Date and End Date

    You're welcpme

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Help in writing formula in excel to produce dates based on start date and end date
    By shoot for moon in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 07-10-2014, 01:00 PM
  2. Replies: 1
    Last Post: 09-12-2012, 10:40 AM
  3. Using VBA to fill col with dates based on start and end date
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-22-2011, 11:58 AM
  4. Looping Generating Specific Dates in Six Month Intervals between end and start date
    By LenaJ887 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-16-2010, 02:59 AM
  5. formula to generate month-to-date based on auto date
    By infinitysales in forum Excel General
    Replies: 1
    Last Post: 06-27-2008, 01:09 AM
  6. Calculate Start Dates based on Need-By Date?
    By GB in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-21-2006, 02:15 PM
  7. [SOLVED] How do I generate a new sheet based on date/week/month?
    By Jay in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-10-2006, 04:55 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1