+ Reply to Thread
Results 1 to 10 of 10

Error Detection

  1. #1
    Registered User
    Join Date
    07-01-2009
    Location
    Maryland
    MS-Off Ver
    Excel 2003
    Posts
    35

    Error Detection

    Ok, so I have built up quite a few macros for automating functions in some spreadsheets, and I want to start being able to detect when errors occur. Right now I just have the function or sub terminate itself if an error occurs, but that just returns control to the calling sub/function.

    I need a way to detect if an error occurs, and immedatly terminate the whole process so that it does not continue and make eronious changes further down the line.

    I had thought of declaring a pubic boolean variable that i could flip if an error occurs, but I am not sure how to detect this flip without programing in an if statement every time i call a function.

    Question1: Is there a way to write a function/sub that will automaticly triger if a public variable becomes a certain value?

    Question2: How do I unload a macro (terminate it) from a public function? I have found that for user forms, the Unload Me command does not work in a public function. I suspect this is becasue the function is not directly attached to a form.

    Thanks in advance!

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Error Detection

    A common practice is to declare procedures as Boolean functions and have them return True if successful.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    07-01-2009
    Location
    Maryland
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: Error Detection

    Quote Originally Posted by shg View Post
    A common practice is to declare procedures as Boolean functions and have them return True if successful.
    I had thought of doing that. But there are a number of functions I am using that return a differnt value.

    For instance, one function is setup to locate an employees record in a look-up table. I want to return the location of the employee from the function. However, if the function does not find the employee, I don't want the calling function to keep processing whatever it needs to do with the record.

    So unfortunatly, having the function return a Boolean wont work :-(

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Error Detection

    A function can (and frequently does) return value(s) in its argument(s).
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    07-01-2009
    Location
    Maryland
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: Error Detection

    Ok, I see how this can work, but it still requires me to turn each and every function call into an If statment. There has to be a cleaner way to check for errors.

    How about this:

    If I define an Error function in each UserForm, and have it display a message and execute Unload Me, is there then a way to call that function from a public function without knowing necesaraly who called the public function.

    So... is there a way for a public function to know who (what function/sub) called it, and then execute a public function from within that calling object?
    Last edited by Pherion; 07-10-2009 at 05:54 PM.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Error Detection

    You can have an error handler in the calling procedure:
    Please Login or Register  to view this content.

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Error Detection

    Provide more information if you like:
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    07-01-2009
    Location
    Maryland
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: Error Detection

    Ok, so I haven't used an error handler before in VB. Does it just display the message, or will it terminate the user form and macro as well.

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Error Detection

    Soo ... try it

  10. #10
    Registered User
    Join Date
    07-01-2009
    Location
    Maryland
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: Error Detection

    Unfortunatly I cant until Monday ;;; I don't have access to my work files at home.

+ 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