DBaker7777
Board Regular
- Joined
- Feb 3, 2009
- Messages
- 53
- Office Version
- 365
- 2016
I have the following code which works great if the user cancels the first inputbox, but I can't seem to get it to work for the enddate if the user selects cancel but enters data into the startdate. Is there a way to write this so if the user selects cancel on either inputbox it exits the sub?
VBA Code:
Sub reportgenerationbasedondates()
Dim lastrow As LongPtr, i As LongPtr, erow As LongPtr
Dim sheetdate As Date, startdate As Date, enddate As Date
Dim response As Variant
response = InputBox("Enter start date as mm-dd-yyyy", "Enter Start Date")
Select Case StrPtr(response)
Case 0
Exit Sub
Case Else
lastrow = Worksheets("Entries").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 Select
End Sub