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

Put the clean up code in a separate sub procedure and call it from
both the main code and the error handler:

Sub CleanUp()
' <clean up code here
End Sub

Sub xxx()
If error then goto Error_handler
' <main code here
CleanUp
Exit sub
Error_handler:
CleanUp
End sub

--

"Shatin" wrote in message ...
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.