+ Reply to Thread
Results 1 to 7 of 7

Error handling in functions within other functions.

  1. #1
    Registered User
    Join Date
    01-27-2011
    Location
    Brazil
    MS-Off Ver
    Excel 2007
    Posts
    75

    Question Error handling in functions within other functions.

    Hello,
    In a sub, I create and format a spreadsheet. Then I call a function that pastes values in this sheet. So I call one last function that creates a graph from these values and, finally, I delete the sheet.
    In each sub/function, I use "On Error GoTo OnErrorCase" to a message. However, if an error occurs within the sub inside the main one, for example (that one that paste data in the sheet), Excel stops, therefore an important part (delete the worksheet at the end) will not run.
    That is, the "On Error GoTo" in the main subroutine doesn't include problems of internal routines. So if I want to "go back" (delete the worksheet in case of error), I must use "mysheet.delete" within each error treatment?
    Last edited by marlonsaveri; 03-09-2011 at 11:23 AM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Error handling in functions within other functions.

    Your last "sub" deletes the temporary sheet? So why not just call that sub as part of your Error handling?
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    01-27-2011
    Location
    Brazil
    MS-Off Ver
    Excel 2007
    Posts
    75

    Unhappy Re: Error handling in functions within other functions.

    No, there's not a sub just to delete the sheet, it's like this:

    Please Login or Register  to view this content.
    Must I write "mytemporarysheet.delete" inside the error handling of each sub?

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Error handling in functions within other functions.

    "Must" is the wrong word. You "can" do it that way. This is not a punishment, it's up to you what you want to handle inside your error handling. If your error handling calls an end to the whole process AND you want the temp sheet still to be deleted at the end, you'll have to design it that way. So putting that delete command into your macro is just as good as any other.

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,021

    Re: Error handling in functions within other functions.

    Why does your routine terminate completely if you have error handling inside each called sub? What does your error handler do?
    Everyone who confuses correlation and causation ends up dead.

  6. #6
    Registered User
    Join Date
    01-27-2011
    Location
    Brazil
    MS-Off Ver
    Excel 2007
    Posts
    75

    Re: Error handling in functions within other functions.

    it does nothing, just a msgbox.

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Error handling in functions within other functions.

    On short, yes, you can put the sheet deletion into the section of your macro(s) that instruct what should happen during error handling.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1