+ Reply to Thread
Results 1 to 6 of 6

Code exits Function at a point where it shouldn't be possible to exit

  1. #1
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,701

    Code exits Function at a point where it shouldn't be possible to exit

    I have attached a workbook that is a skeletal version of a workbook that takes raw data from timesheet records and generates reports for a set of projects. I've stripped out all the code except the setup portion where it deletes all the worksheets that were generated the last time the macros were run. The worksheets being deleted each correspond to a project. The other worksheets are identified as static worksheets, meaning that they should always be there and therefore should not be deleted.

    I have reduced the code down to only that code causing a problem. The command button on the Controls sheet invokes a Sub in Module Main called CreateReports. That Sub calls ResetSheets, which deletes all non-static sheets. It in turn calls a function in Module Exclude called StaticSheet that compares a sheet name to a list of static sheets, and returns a Boolean indicating whether the given sheet is static. If it is static, it is not deleted.

    The problem is that execution in the function StaticSheet is abandoned prematurely. There is a comment at the line of code where this happens. There is nothing at that point in the code that should allow this Function to be exited. There is no error, it just continues execution in the caller. The return value of the function is never set, therefore it always returns False.

    This problem occurs regardless of whether I run the code normally, or step through it using F8. The file is attached but here is all the code:

    Module Main:
    Please Login or Register  to view this content.
    Module Exclude:
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,701

    Re: Code exits Function at a point where it shouldn't be possible to exit

    I just noticed that I used the non-existent attribute Hidden. I changed it to Visible, which seems to resolve the issue, although I can't understand why I it is not raising an error.
    Last edited by 6StringJazzer; 10-21-2014 at 04:51 PM.

  3. #3
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: Code exits Function at a point where it shouldn't be possible to exit

    although I can't understand why I it is not raising an error.
    Weird indeed, error 438 was raised when I stepped thru it.
    Cheers!
    Tsjallie




    --------
    If your problem is solved, pls mark the thread SOLVED (see Thread Tools in the menu above). Thank you!

    If you think design is an expensive waste of time, try doing without ...

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Code exits Function at a point where it shouldn't be possible to exit

    What's your setting for Error Trapping (Tools>Options>General Tab)?

    Mine is break on all errors and that function breaks with an 'Object doesn't support...' error on the 'Hidden' line of code.
    If posting code please use code tags, see here.

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,701

    Re: Code exits Function at a point where it shouldn't be possible to exit

    My option is set at "Break on unhandled errors". Upon further review it looks like what is happening is the Resume Next in the caller is throwing away the error that is raised to it by the Function. Mystery solved.

    Thanks for your input.

    And Norie, congratulations on your 12,000th post

  6. #6
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: Code exits Function at a point where it shouldn't be possible to exit

    it looks like what is happening is the Resume Next in the caller is throwing away the error
    Well, actually it's a combination of the resume next and the setting of the error trapping.
    With resume next on and error trapping set to "break on unhandled errors" there will be no error message. Setting "break on all errors" will also raise an error when resumen next is on.
    On the other hand: why not just remove the resume next. It's something to be careful with and - afaiks - there's no reason for setting it here.

    BTW, thx for the rep
    Last edited by Tsjallie; 10-22-2014 at 04:19 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Macro exits prematurely from function after calling other function
    By LouisPhilippe in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-22-2013, 03:22 AM
  2. [SOLVED] Loop Code skips rows eventhough it shouldn't
    By questionguy in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-13-2012, 08:58 AM
  3. VBA Code shouldn't export header information
    By Magerator in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-19-2010, 08:52 AM
  4. VBA bug - code exits sub before completion - no error message
    By stefsj in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 05-28-2010, 01:26 PM
  5. Custom Function being called when it shouldn't.
    By Ron in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-18-2005, 04:06 PM

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