MrExcel Publishing
Your One Stop for Excel Tips & Solutions

On error... errors


Posted by Brian Charles on February 14, 2000 10:07 AM

I am using the on Error statement as below:

Dim codelist as Variant
Codelist=Array(10,21,29,30,40)
For each code in codelist
On error GOTO D:
WIndows(code).activate

(A buch of code follows)

D: Next code
On error goto 0

Prior to this code running, any number of windows are open
that have names that are within the codelist. THe problem is
that when the code has an error due to attempting to
open a window not listed, it works fine on the first error
but stops on the subsequent error.

ie: codes(21,29,30) are open, macro tries to open
code 10, skips over fine. THen tries to open code 40,
and grinds to a halt.

Help if you can.....



Posted by kaiowas on February 15, 2000 6:39 AM

Dealing with an error has to be a bit more structured. Basically once you encounter an error execution of the main code is suspended while you jump to another section of code with deals with the problem then you must tell VB that the error has been fixed and it is OK to carry on. In your example this would be done as follows...
.
.
'the rest of your routine then.....
.
.
exit sub 'this isolates your error handling routine from the rest of the code

errortrap:
resume d
end sub


In more complicated code the error handling routine would generally include an if statement which uses 'err' and 'erl' to determine what error has occurred. You can also do other things before the resume statement such as writing to an error log. For more information of different ways of using 'resume' look in the VB help files

Posted by Ivan Moala on February 16, 2000 2:50 AM

Brian
Try changing it to;

(A buch of code follows)

D:If Err.Number <> 0 Then Resume Next
Next code
On Error GoTo 0


Ivan