error Handling in loop "For"

radian89

Board Regular
Joined
Nov 12, 2015
Messages
113
Hi Y'all

i know this is silly, but i can't figure it out. please help :

Code:
for i = 1 to 2

   on error goto ErrHandler

   'my code here
   sheets("Panel1").range("A1").value = "Success"

   errHandler :
   sheets("Panel1").range("A1").value = "Failure"
   resume nextitiration

nextitiration :

next i

i want to add "Success" to my cell, if it's successfully executed. but somehow even there's no error, my errHandler still run, so it gives "Failure".
how to do this properly?

thanks a lot
warm regards

Adrian
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
It's hard to tell exactly what you want from that snippet, but you don't want to put the error handler within the loop, that's why it's always executed. Try something like:

Code:
Sub test1()

    On Error GoTo errHandler
    For i = 1 To 2
    
   'my code here
   Sheets("Panel1").Range("A1").Value = "Success"

nextiteration:

    Next i

Exit Sub

errHandler:
   Sheets("Panel1").Range("A1").Value = "Failure"
   Resume nextiteration


End Sub
 
Upvote 0
It is going to hit that every time, because nothing is telling it to skip over it.

You typically put the error handler outside of the loop, at the bottom of your code.
And then you put a line just above it that says:
Code:
Exit Sub

So, if there are no errors, it will hit that line and exit the sub without going on to the Error Handling code.
 
Upvote 0
It's hard to tell exactly what you want from that snippet, but you don't want to put the error handler within the loop, that's why it's always executed. Try something like:

Code:
Sub test1()

    On Error GoTo errHandler
    For i = 1 To 2
    
   'my code here
   Sheets("Panel1").Range("A1").Value = "Success"

nextiteration:

    Next i

Exit Sub

errHandler:
   Sheets("Panel1").Range("A1").Value = "Failure"
   Resume nextiteration


End Sub


Hi, thanks for quick reply. what about if the variable itself inside the loop like this :
sorry not telling you that earlier, but this is now exactly like i've been dealing with.
if i put the errorhandler it gives error 9 bcs, it's outside the loop

Code:
Sub test1()

set test(1) = Sheets("Panel1")
set test(2) = Sheets("Panel2")

For i = 1 To 2
    On Error GoTo errHandler    
   'my code here
   test(i).Range("A1").Value = "Success"



errHandler:
   test(i).Range("A1").Value = "Failure"
   Resume nextiteration

nextiteration:

    Next i


End Sub
 
Upvote 0
You can still do it like this, the value of i is maintained in the error handler:

Rich (BB code):
Sub test1()

Set test(1) = Sheets("Panel1")
Set test(2) = Sheets("Panel2")

On Error GoTo errHandler
For i = 1 To 2
   'my code here
   test(i).Range("A1").Value = "Success"

nextiteration:

    Next i
    
    Exit Sub

errHandler:
   test(i).Range("A1").Value = "Failure"
   Resume nextiteration

End Sub

If you REALLY want (I don't recommend this version), you can add a GoTo to skip around the error handler and leave it in the loop:

Rich (BB code):
Sub test1()

Set test(1) = Sheets("Panel1")
Set test(2) = Sheets("Panel2")

For i = 1 To 2
    On Error GoTo errHandler
   'my code here
   test(i).Range("A1").Value = "Success"
    GoTo nextiteration:

errHandler:
   test(i).Range("A1").Value = "Failure"
   Resume nextiteration

nextiteration:

    Next i

End Sub

I don't recommend it because it makes the code harder to read. Also, the On Error should be outside the loop, it doesn't need to executed every time.
 
Last edited:
Upvote 0
Hi @Eric W...

that works like charm....

anyway in REALLY need that solution for now, is there in anyway maintain the value of i, other than this way?

thanks so much
Best Regards

Adrian
 
Upvote 0
I'm not sure what you're asking. Where/why/how do you want to maintain the value of i? Within the loop, the For/Next handles the value. If you exit the loop early, i will maintain the last value it had in the loop. If you let the loop end normally, the value of i will be the maximum loop value + 1. You should never jump into a loop other than at the start of it (except for the error handler).
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,876
Members
452,363
Latest member
merico17

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