Hi,
I have the following code to search WB2 for data in a date range and place the data from each row into WB1. The issue I am having is if the 'Cancel' button on the 'InputBox' is pressed it just errors and drops me back into the code. I've tried alsorts but can't seem to get round this.
If you enter dates in the two InputBox's then it works perfectly.
Thanks in advance.
I have the following code to search WB2 for data in a date range and place the data from each row into WB1. The issue I am having is if the 'Cancel' button on the 'InputBox' is pressed it just errors and drops me back into the code. I've tried alsorts but can't seem to get round this.
If you enter dates in the two InputBox's then it works perfectly.
Thanks in advance.
Code:
Private Sub CmdTest_Click() 'Test Run Report
Dim wbk1 As Workbook
Dim sht1 As Worksheet
Dim wbk2 As Workbook
Dim sht2 As Worksheet
Dim startdate As Date, enddate As Date
Dim rng As Range, destRow As Long
'Dim shtSrc As Worksheet, shtDest As Worksheet
Dim c As Range
Application.ScreenUpdating = False 'Stops screen from showing during process
Set wbk1 = ThisWorkbook
Set sht1 = wbk1.Sheets("Report Data")
Set wbk2 = Workbooks.Open("\\GLYNN\Shared\XXXX Returns v.1.0.xlsx", ReadOnly:=True)
Set sht2 = wbk2.Sheets("XXXX Returns")
destRow = 2 'start copying to this row
startdate = CDate(InputBox("Enter Start Date"))
enddate = CDate(InputBox("Enter End Date"))
'don't scan the entire column...
Set rng = Application.Intersect(sht2.Range("D:D"), sht2.UsedRange)
For Each c In rng.Cells
If c.Value >= startdate And c.Value <= enddate Then
'Starting one cell to the right of c,
' copy a 5-cell wide block to the other sheet,
' pasting it in Col H on row destRow
c.Offset(0, -3).Resize(1, 12).Copy _
sht1.Cells(destRow, 1)
destRow = destRow + 1
End If
Next
wbk2.Close savechanges:=False 'Closes Master workbook without saving
Application.ScreenUpdating = True 'Re-enables ScreenUpdating
End Sub