+ Reply to Thread
Results 1 to 17 of 17

error trapping not working?

  1. #1
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    error trapping not working?

    Hello,

    I have the following code in VBA:

    Please Login or Register  to view this content.
    My error catching is just that line " on error goto error_catching, and at the bottom of the code, I have

    Please Login or Register  to view this content.
    all of my codes are in the worksheet_change event.

    When I run it, say for example, I type in....7/4/abdf instead of 7/4/2011, VBA still breaks into debug mode without invoking the error handler.

    Is there a way around this?

    Thank you
    Last edited by Lifeseeker; 01-10-2012 at 11:08 PM.

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,598

    Re: error trapping not working?

    on error... has to preceed the code line that may trigger an error, i.e. the addition line.
    Ben Van Johnson

  3. #3
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: error trapping not working?

    so far I have..

    Please Login or Register  to view this content.
    and at the top, i have
    Please Login or Register  to view this content.
    When I type in 7/4/abcd, I have the msgbox pops up twice, but then VBA run-time window appears. How do I make it so that the VBA run-time error window doesn't appear at all, and after seeing the message, users would then make the correction reasonably?

    By the way, I"m not sure why the msgbox popps twice.

    Any ideas?

  4. #4
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: error trapping not working?

    anybody able to assist?

  5. #5
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,598

    Re: error trapping not working?

    Can you post the complete code? Error handlers are put at the end, not at the top, of a module and usually include what to do when the error is fixed.

  6. #6
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: error trapping not working?

    You also need to ensure that you have an Exit Sub line before the error handler section to ensure it doesn't get run every time even if there is no error.
    Good luck.

  7. #7
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: error trapping not working?

    Quote Originally Posted by protonLeah View Post
    Can you post the complete code? Error handlers are put at the end, not at the top, of a module and usually include what to do when the error is fixed.
    THere you go. Please see the attached.

    In worksheet "Test", so if you type in cells in col 7 or 8, or G or H, for example, 7/4/abcd, it will go to the problem.

    I'm basically combining the date with the time.

    Thanks
    Attached Files Attached Files

  8. #8
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: error trapping not working?

    Quote Originally Posted by OnErrorGoto0 View Post
    You also need to ensure that you have an Exit Sub line before the error handler section to ensure it doesn't get run every time even if there is no error.
    Hi,

    Would you mind taking a look at it please?

  9. #9
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: error trapping not working?

    Please Login or Register  to view this content.



  10. #10
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: error trapping not working?

    Hi snb,

    When I replace my code with yours, I still get the type mismatch run-time error. This is because I have set the column 7's format to be date and column 8's format to be 24hrs time.

    Any ideas?

    My concern was...I would prefer that VBA continues to run even when it has encountered a problem. How do I make it so that the normal flow of the program won't be interrupted?

    Thanks
    Attached Files Attached Files

  11. #11
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: error trapping not working?

    You should structure the error trapping like this

    Please Login or Register  to view this content.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  12. #12
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: error trapping not working?

    This is not about formatting but about values.

    Please Login or Register  to view this content.

  13. #13
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: error trapping not working?

    hi there,

    Right now, if I type in 7/4/201b, the program does tell users where the error has occurred and I am able to go in and correct the error.

    However, after a correction is made, the program doesn't seem to be going forward, and it seems to be hung at the previous error even though the error has now been corrected.

    Is there a way around this?

    Thank you
    Attached Files Attached Files
    Last edited by Lifeseeker; 01-09-2012 at 04:30 PM. Reason: re-explanation the issue

  14. #14
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: error trapping not working?

    Is anyone able to assist?

  15. #15
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: error trapping not working?

    I think you are trying to detect an error in the user's input. The error trapping in the code is designed to detect errors in the code.
    Last edited by royUK; 01-10-2012 at 12:29 PM.

  16. #16
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: error trapping not working?

    You might use something like
    Please Login or Register  to view this content.

  17. #17
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: error trapping not working?

    wow nice, worked like a charm.

    Actually I added one line as it the .clearcontents doesn't appear to clear the combined date/time field at the later column, which might cause inconsistencies in data. But overall thank you again!

    Please Login or Register  to view this content.

+ 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