The Resume Statement


The Resume statement can be added to make the code execution branch back to the statement where the error occurred. This gives the opportunity for the user to intervene ‚ for example, to put a floppy disk into drive A and for the code to then reinterrogate drive A. You can include this in your error-handling routine so that the line that created the error will be tried again following user intervention:

 Sub Test_Error() 
On Error GoTo err_handler
temp = Dir("a:\*.*")
Exit Sub
err_handler:
If Err.Number = 71 Then
MsgBox "The A drive is not ready"
Else
MsgBox "An error occurred"

End If
Resume
End Sub

You can also add the statement Next to Resume . This will skip over the statement that created the error and therefore ignore the error.

 Sub Test_Error() 

On Error Resume Next

temp = Dir("a:\*.*")

End Sub

If there is no disk in drive A, the code will still run perfectly because of On Error Resume Next ‚ it skips over the line of code creating the error.

Resume Next can be useful for dealing with errors as they occur, but it can make debugging code very difficult. In a later stage of your program, you may have incorrect or nonexistent data being produced due to the fact that an error condition earlier was ignored. You can end up with a program that appears to run OK, but in fact does nothing because it has some hidden bugs or incomplete data. This is because every time an error is encountered , the execution just skips over it. This can give a false impression of what is actually happening, so if you do use On Error Resume Next , make sure that you check all inputs and outputs to the code to ensure that everything is working as it should be. Make sure that your On Error Resume Next statement cannot cover up an error in a read from a spreadsheet cell or from a file. This could cause disaster in your program because the On Error Resume Next statement would make it appear to work perfectly.




Excel VBA Macro Programming
Excel VBA Macro Programming
ISBN: 0072231440
EAN: 2147483647
Year: 2004
Pages: 141

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net