VBA - Behavior of On Error Resume <Label> within For Loop

patkim

New Member
Joined
Jul 23, 2006
Messages
21
I am completely intrigued by this apparently a very simple code. I guess I need to understand how really On Error GoTo <Label> works when put within a For Next loop.

This code obviously goes into runtime error at j = 7/0. However on the first iteration For i = 1, it successfully goes to Label myerr, shows the MsgBox and returns to For loop for next iteration. This time it again encounters On Error GoTo myerr statement but this time it seems to ignore this and just fall thru the error with runtime error at j = 7/0.

I find it hard to understand this behavior. Can someone explain why is it so? Why does the code ignore On Error GoTo myerr in the next iteration?
Instead if I put On Error Resume Next either inside or before For loop it does skip it every time correctly. Thanks.

Sub Onerror()
For i = 1 To 3
On Error GoTo myerr
MsgBox (i)
j = 7 / 0

myerr:
MsgBox ("error")
Next i
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

Thanks for the link. The article however does not describe how On Error GoTo <Label> behaves when placed within a For loop. It just shows how to place the label just before the end of the Sub which is very straight forward approach and upon processing exit the sub, that's what all VBA books also talk about.

This is somewhat confusing because despite of executing the On Error Goto <label> statement in the second iteration of For loop, VBA does not follow it. It just falls thru the error.

Thanks.
 
Last edited:
Upvote 0
I will get told off for this but look at the -1 argument (it is covered in the link) i.e.

Code:
Sub Onerror()
    Dim i As Long, j As Long
    For i = 1 To 3
        On Error GoTo myerr
        MsgBox (i)
        j = 7 / 0

myerr:
        MsgBox ("error")
        On Error GoTo -1
    Next i
End Sub
 
Upvote 0
If you read that article posted then look at the section for "On Error GoTo -1". It explains that when you use the code "On Error GoTo <Label>" then you have set off the mousetrap that is the error trap, but just going to the label doesn't reset the error trap.


Try this:-

Code:
Sub Onerror()
 For q = 1 To 3
 On Error GoTo myerr
 MsgBox (q)
 j = 7 / 0
myerr:
 MsgBox ("error")
 On Error GoTo -1
 Next q
End Sub

To call this inelegant would be an understatement!
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,401
Messages
6,124,705
Members
449,182
Latest member
mrlanc20

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