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
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