Note that there are some explanatory texts on larger screens.

plurals
  1. POVBA- Handling Errors in all procedures with a UDF
    primarykey
    data
    text
    <p>I am dealing with a good amount of workbooks with many procedures. The deeper I go in my project, the more I try to write functions or sub to process common tasks. First of all, as the final user will be an average Excel user, I decided to protect and centralize all my code in a <a href="http://office.microsoft.com/en-us/excel-help/copy-your-macros-to-a-personal-macro-workbook-HA102174076.aspx" rel="nofollow noreferrer">personal macro workbook</a>. It sounds to me a good idea, but maybe some of you, Excel gurus, don't agree. Then I wrote a <strong>Public Sub ErrorHandler ()</strong> to deal with errors in all my procédures. I am fully aware of the <a href="https://stackoverflow.com/questions/1418777/vba-error-bubble-up/1419147#1419147">Error Bubble Up</a>. My procedure will be soemthing like this:</p> <pre><code>' All déclarations are outside Public Sub ErrorHandler () Lg1 = Err.Number ' Lg1 is Long Select Case Lg1 Case 1004 MsgBox ("File is not fund. Please verify its path.") Exit Sub '!EDITED! &lt;GoTo NextCode:&gt; is much better indeed ' ...more code with other errors... End Select NextCode: ' !EDITED! ' ...some cleaning and initialize variables to nothing or 0 Lg1 = 0 Err.Clear End Sub </code></pre> <p>Then I would like to use this sub in other sub this way :</p> <pre><code>On Error Go To MyError: ' &lt;On Error call ErrorHandler&gt; is not permited '...more code.... MyError: call ErrorHandler </code></pre> <p>Now a few questions:</p> <ul> <li>Is it a good practice ? If not, what would you recommend ?</li> <li>In case 1004 for example, when I say <em>Exit sub</em>, will the <em>ErrorHandler sub</em> itself go to end and do the cleaning stuff, or will it stop too ?</li> <li>Any good hints about common and useful user defined errors (513-65535) ?</li> </ul> <p>Thank you for help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload