How to generate a report based on dates with VBA. Watch the video below:
Watch this video on YouTube.
Here’s the complete VBA code to generate a report from data in sheet1 to another sheet2 based on dates:
Sub reportGenerationBasedonDates()
Dim lastrow As Long, i As Long, erow As Long
Dim sheetdate As Date, startdate As Date, enddate As Date
lastrow = Worksheets(“sheet1”).UsedRange.Rows.Count
startdate = InputBox(“Enter start date as mm-dd-yyyy”, “Enter start date”)
enddate = InputBox(“Enter the end date as mm-dd-yyyy”, “Enter end date”)
For i = 2 To lastrow
sheetdate = Cells(i, 1)
If sheetdate >= startdate And sheetdate <= enddate Then
erow = Worksheets(“sheet2”).UsedRange.Rows.Count + 1
Range(Cells(i, 1), Cells(i, 5)).Copy Destination:=Sheets(“sheet2”).Cells(erow, 1)
End If
Next i
End Sub
Further reading:
How to use dates and times in Excel