Error handling

gavinkelly

Board Regular
Joined
Jan 12, 2008
Messages
220
I have having trouble with error handling:
Code:
On Error GoTo ErrorHandler
For y = 2 To 25

    For x = 2 To 9
        i = WorksheetFunction.Find(Cells(x, 4), Cells(y, 1), 1)
        Cells(y, 2) = Cells(x, 5)
        Exit For
ErrorHandler:
     Next x
Next y

The first error is ignored as it goes to ErrorHandler and so resumes the loop but the next time there is an error in the loop the code stops i.e. it does not go to ErrorHandler. I guess this is to do with errors in loops.

any suggestions to make the error always got to the handler?
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
24,348
Rather than using the spreadsheet FIND, you could use the VB InStr and not need error handling.

Code:
For y = 2 To 25
    For x = 2 To 9
        If 0 < InStr(Cells(y,1),Cells(x, 4)) Then
            Cells(y, 2) = Cells(x, 5)
            Exit For
        End If
     Next x
Next y
 
Upvote 0

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
40,432
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
If you want to go back to normal code processing from an error handler, you have to use the Resume statement.
 
Upvote 0

gavinkelly

Board Regular
Joined
Jan 12, 2008
Messages
220
I am unsure how to use the Resume statement to make it work, i have tried excel VBA help but i am still struggling. Where does it go in my code?
 
Upvote 0

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
40,432
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
It doesn't really - I was just making a general point. You would be better off using the version provided by mikerickson above, but since you effectively want to just ignore the error you could use On Error Resume Next:
Code:
On Error Resume Next
For y = 2 To 25

    For x = 2 To 9
        i = WorksheetFunction.Find(Cells(x, 4), Cells(y, 1), 1)
        If Err.Number = 0 then
            Cells(y, 2) = Cells(x, 5)
            Exit For
        Else
            Err.Clear
        End If
     Next x

Next y
though it would be quicker to use the range.Find method rather than looping.
</pre>
 
Upvote 0

Makrini

Well-known Member
Joined
May 22, 2007
Messages
1,035
2 ways..

Either in your normal code before running what could cause an error...

On Error Resume Next


Or...

On Error Goto ErrorHandler

code..

ErrorHandler:
Resume Next


but the previous poster was correct - use Instr function so you don't have to rely on Error catching in this case
 
Upvote 0

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
40,432
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
FYI, the reason you have to use Resume if you use an error handler section, is that if you don't, VBA considers the error handler code to still be running. If an error is raised within an error handling routine, you just get a run time error (otherwise you would end up in an infinite loop).
 
Upvote 0

Forum statistics

Threads
1,191,614
Messages
5,987,680
Members
440,104
Latest member
thigarette

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
Top