VBA Error Handling

CROY1985

Well-known Member
Joined
Sep 21, 2009
Messages
501
Office Version
  1. 365
Platform
  1. Windows
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.


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
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
As you haven't included the error handler code, it's difficult to help.
But I'd recommend you read this it has pretty much everything you need to know about handling errors.
 
Upvote 0
There are better ways to handle this anyway, without an error handler, such as testing to see if you found what you were looking for:

Code:
    Set where = C.Find(what:=searchfor, after:=C(1))
    If not where is nothing then
       startrow = where.Row
       Set where = C.Find(what:=searchfor, after:=C(1), searchdirection:=xlPrevious)
       endrow = where.Row
       Range(startrow & ":" & endrow).Copy ThisWorkbook.Sheets("Dataset").Cells(ThisWorkbook.Sheets("Dataset").UsedRange.Rows.Count + 1, 1)
End If
 
Upvote 0
Basically: When the error happens you must reset the error handler in preparation for a possible next error. You do this with On Error Goto 0.
Read the link provided by Fluff. It explains this and much more.
 
Upvote 0
Basically: When the error happens you must reset the error handler in preparation for a possible next error. You do this with On Error Goto 0.
Read the link provided by Fluff. It explains this and much more.
Whenever possible, it is usually preferrable to avoid the error altogether, like Rory demonstrated.
In this case, it is pretty easy to avoid the error.
 
Upvote 0
To clarify, On Error Goto 0 is not enough to reset an active exception. You need a Resume statement (or On Error Goto -1 if you like writing horrible code)
 
Upvote 0

Forum statistics

Threads
1,214,970
Messages
6,122,514
Members
449,088
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top