Hi all
I have a routine for which i include some error handling.
It basically loops through sheets, and then for each sheet loops through a number of filters and copies data across.
The first time there is an error it uses the go to errhandler2, however the second time it skips the error handler and i get Runtime Error 91 Object Variable or With Variable not set. I want it to go to errhandler2 each time it encounters an error: not just the first time.
The error occurs at:
on startrow = where.Row (i.e startrow = nothing, at that point in the routine)
Any ideas?
Thanks
Chris
I have a routine for which i include some error handling.
It basically loops through sheets, and then for each sheet loops through a number of filters and copies data across.
Code:
For weekx = weekfrom To Weekto
ThisWorkbook.Activate
datasheetname = Sheets("Weeks").Cells(weekx, 1).Value & ".xlsx"
Datasheet = salesdatasheetpath & datasheetname
On Error GoTo ErrHandler1
Workbooks.Open Filename:=Datasheet
Call SortDatasheet
For filterx = 1 To Filterto
On Error GoTo ErrHandler2
MsgBox datasheetname
Workbooks(datasheetname).Activate
MsgBox ThisWorkbook.Sheets("Control").Cells(11 + filterx, 1)
searchfor = ThisWorkbook.Sheets("Control").Cells(11 + filterx, 1)
Set C = Range("D:D")
Set where = C.Find(what:=searchfor, after:=C(1))
startrow = where.Row
Set where = C.Find(what:=searchfor, after:=C(1), searchdirection:=xlPrevious)
endrow = where.Row
Range(startrow & ":" & endrow).Copy
ThisWorkbook.Activate
Sheets("Dataset").Select
Cells(ActiveSheet.UsedRange.Rows.Count + 1, 1).Select
ActiveSheet.Paste
Workbooks(datasheetname).Activate
Continue:
Next filterx
Workbooks(datasheetname).Close
ThisWorkbook.Activate
Next weekx
The first time there is an error it uses the go to errhandler2, however the second time it skips the error handler and i get Runtime Error 91 Object Variable or With Variable not set. I want it to go to errhandler2 each time it encounters an error: not just the first time.
The error occurs at:
on startrow = where.Row (i.e startrow = nothing, at that point in the routine)
Any ideas?
Thanks
Chris