View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Shatin Shatin is offline
external usenet poster
 
Posts: 54
Default Error Handling and Cleanup

In writing a macro, I've read that it's good programming practice to clean
up after the macro is run, by that I mean resetting
application.screenupdating, calculation modes to their original states, that
sort of thing. Now in the case of there being some code for error handling,
that code is typically placed at the end of the macro:

Sub xxx()

main code
If error then goto Error_handler
clean up code
Exit sub

Error_handler:
code
Exit sub

End sub

There's a chance that after handling the error, the macro may be exited
without there being any cleanup. I suppose one can repeat the cleanup code
in the error handler. However, if there are more than a few errorhandlers to
deal with different types of errors, it would be clumsy to repeat the same
cleanup code again and again. How should this problem be dealt with?

TIA.