+ Reply to Thread
Results 1 to 18 of 18

Error Handling in Excel Macro

  1. #1
    Registered User
    Join Date
    05-19-2012
    Location
    Fort Worth TX
    MS-Off Ver
    Excel 2010
    Posts
    13

    Error Handling in Excel Macro

    I have been trying to figure out how to make an Excel 2010 macro not give me an error when "find" does not find what it is looking for.

    I added Error Handler at the front of the macro and Error Handler "resume next" after the "find", but I need it to skip all the finds and go to a specific line, not just resume. Thanks very much for any help.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,446

    Re: Error Handling in Excel Macro

    You need On error Resume Next BEFORE the action that may error and On Error Goto 0 afterwards.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    05-19-2012
    Location
    Fort Worth TX
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Error Handling in Excel Macro

    TMS, Your answer worked perfectly. I could have saved hours of searching and working to find the answer had I just posted this before. Thank You, Thank You, Thank You.

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,446

    Re: Error Handling in Excel Macro

    You're welcome. Thanks for the rep. You learn by trying and failing so the time was not all wasted

  5. #5
    Registered User
    Join Date
    05-19-2012
    Location
    Fort Worth TX
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Error Handling in Excel Macro

    Thanks again you must be an optimistic person. I guess my time wasn't wasted after all. Is there a way to have the macro upon error, go to a specific line further down? Or maybe even run a different macro? The resume next works great, but I also need to make the macro skip further instructions upon error. Thanks again.

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,446

    Re: Error Handling in Excel Macro

    Sure, you'd use something like:

    Please Login or Register  to view this content.

    This is just a simplistic approach. You'd need to be more specific about what you are trying to do, what errors you might need to process, and what you want to do when you get one in order for anyone to offer a practical solution.

    Best bet is to look for examples of error handling routines. I rarely (close to never) use On "Error Goto Label"

    Regards, TMS

  7. #7
    Registered User
    Join Date
    05-19-2012
    Location
    Fort Worth TX
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Error Handling in Excel Macro

    Many thanks. Could I send you a simple excel spreadsheet with proposed macro so you can see what I am talking about?

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,446

    Re: Error Handling in Excel Macro

    You're better uploading it to the forum so more people can offer help.

    Regards, TMS

  9. #9
    Registered User
    Join Date
    05-19-2012
    Location
    Fort Worth TX
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Error Handling in Excel Macro

    OK great. I'll do that.

    Thanks again

  10. #10
    Registered User
    Join Date
    05-19-2012
    Location
    Fort Worth TX
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Error Handling in Excel Macro

    On your code:

    On Error Goto ErrorHandler
    ' your code (what code do I use here to stop the macro and go to the ErrorHandler?)
    '

    Exit Sub

    ErrorHandler:

    I put for my code:
    Application.Run "Book2!Macro7"

    I put this before and after the line that will eventually cause the error (a "find" search). The ErrorHandler goes to Macro 7 whether there is an error or not.

    Much appreciation!

  11. #11
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Error Handling in Excel Macro

    As TMShucks stated you should upload a workbook. With code tags
    Please Login or Register  to view this content.
    is where the error is caught,
    Please Login or Register  to view this content.
    is where you put the code where you want the error to be handled, example
    Please Login or Register  to view this content.
    If you want something more specific, upload a sanitized workbook with mock data with a before and after scenario too. This will help us help you tremendously.

    Cheers
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  12. #12
    Registered User
    Join Date
    05-19-2012
    Location
    Fort Worth TX
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Error Handling in Excel Macro

    Thanks again, here is the file. The scenario is on the workbook.
    Attached Files Attached Files

  13. #13
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,446

    Re: Error Handling in Excel Macro

    Actually, for this specific example, you don't need error handling:

    Please Login or Register  to view this content.

    Regards, TMS

  14. #14
    Registered User
    Join Date
    05-19-2012
    Location
    Fort Worth TX
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Error Handling in Excel Macro

    Your test works exactly right. Your help is appreciated more than I can tell you. Obviously, I am not an accomplished Excel programmer, but I can usually get things done with my limited knowledge. My way almost always causes the macro to be cumbersome and slow because of my limits, but it usually works.

    I tend to obsess about things I cannot figure out. For future reference and so I can sleep at night, would it be possible to let me know the code for the errorhandler method that I was trying? I won't use it now, but it might come in handy in the future. Again, thanks so much for your time.

  15. #15
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,446

    Re: Error Handling in Excel Macro

    would it be possible to let me know the code for the errorhandler method that I was trying?
    To be honest, no. Not because I'm trying to be difficult, just because I don't use it so I can't really quote an example which I feel confident would work. I have already outlined the method and Mordred has clarified the mechanics.

    Chip Pearson has a very detailed explanation of Error Handling here: Error Handling

    As I said, I tend to adopt the "On Error Resume Next" approach, test the error condition and, if necessary, take action in the next lines, and switch off the error handling with "On Error Goto 0". This means the error trapping is very localised, I'm in control, and it's switched off again as soon as possible. That, in turn, means that, if I get an error that I wasn't expecting, the code will crash ... but then the problem is highlighted and can be fixed ... albeit with some more error handling.

    Regards, TMS
    Last edited by TMS; 05-21-2012 at 07:01 AM.

  16. #16
    Registered User
    Join Date
    05-19-2012
    Location
    Fort Worth TX
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Error Handling in Excel Macro

    Understood. Thanks again for your help.

  17. #17
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Error Handling in Excel Macro

    btw, you also don't need error handling if you use Find. rather than
    Please Login or Register  to view this content.
    use
    Please Login or Register  to view this content.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  18. #18
    Registered User
    Join Date
    05-19-2012
    Location
    Fort Worth TX
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Error Handling in Excel Macro

    Thanks, all the help is appreciated.

+ 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