Generating a list of dates given the start and end dates
date, excel, vba
Solution
EDIT:
This is apparently what you need, as discussed in comments.
Sub GenerateDates()
Dim FirstDate As Date
Dim LastDate As Date
Dim NextDate As Date
FirstDate = Range("startdate").Value
LastDate = Range("enddate").Value
NextDate = FirstDate
Range("tripdays").Select
'selection of columns within one row
Do Until NextDate > LastDate
ActiveCell.Value = NextDate
ActiveCell.Offset(1, 0).Select
NextDate = NextDate + 1
Loop
End Sub
Alternatively, a `For` loop would do just as well.
Screenshot:
FURTHER EDIT:
Horizontal version, as requested.
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("tripdays")
For DateIter = FirstDate To LastDate
DateOffset.Value = DateIter
Set DateOffset = DateOffset.Offset(0, 1)
Next DateIter
End Sub
Screenshot:
Note: I've also fixed the vertical version to stop at the end date provided.
Problem
Previously I found some VBA code done by Andy Brown that generates a list and makes each date the first or 15th for another user. I have tried to adjust this code to my needs but I'm struggling. Currently the code, once run, is just putting in the same date over and over and I have to end Excel. ``` Sub GenerateDates() Dim FirstDate As Date Dim LastDate As Date Dim NextDate As Date FirstDate = Range("A1").Value LastDate = Range("a2").Value NextDate = FirstDate Range("B1").Select Do Until NextDate >= LastDate ActiveCell.Value = NextDate ActiveCell.Offset(1, 0).Select If Day(NextDate) = 1 Then NextDate = DateAdd("d", NextDate, 14) Else NextDate = DateAdd("d", NextDate, 20) NextDate = DateSerial(Year(NextDate), Month(NextDate), 1) End If Loop ``` Previous code I based my model upon is listed above and my, most likely terrible code, is below: ``` Sub GenerateDates() Dim FirstDate As Date Dim LastDate As Date Dim NextDate As Date FirstDate = Range("startdate").Value LastDate = Range("enddate").Value NextDate = FirstDate Range("tripdays").Select 'selection of columns within one row Do Until NextDate >= LastDate ActiveCell.Value = NextDate ActiveCell.Offset(1, 0).Select If Day(NextDate) = 1 Then NextDate = DateAdd("d", NextDate, 14) End If Loop End Sub ``` What I need instead is to generate every date between the given start and end dates, instead of just the 15th and 1st. How is this done?