+ Reply to Thread
Results 1 to 13 of 13

VBA Error Handling

  1. #1
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    VBA Error Handling

    Hi, I wonder whether someone could help me please.

    I've put together the script below where I'm trying to implement the 'Error Handling'.

    Please Login or Register  to view this content.
    What I'm trying to achieve is:
    • Exit the macro with the error message, and
    • Closing the user form and the iMessage if the file path doesn't exist.

    The problem I have is that the error message isn't displayed and the iMessage is displayed, so I've clearly gone wrong.

    I just wondered whether someone may be able to look at this please and offer some guidance on how I can resolve this.

    I have also used the Stackoverflow site at this post http://stackoverflow.com/questions/2...lder-existence but unfortunately I'm still having issues.

    Many thanks and kind regards

    Chris
    Last edited by hobbiton73; 04-30-2015 at 03:57 AM.

  2. #2
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: VBA Error Handling

    First there's a typo here:
    Please Login or Register  to view this content.
    which the compiler really ought to tell you.

    Second, where exactly would the error occur?
    Remember what the dormouse said
    Feed your head

  3. #3
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Error Handling

    Hi @ romperstomper, thank you for coming back to me with this and I noticed the typo.

    The error would occur where the opposite occured here:
    Please Login or Register  to view this content.
    i.e where fpath = "".

    Many thanks and kind regards

    Chris

  4. #4
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: VBA Error Handling

    That's not an error, it's just the Else part of the If clause. It also can't possibly happen since you hardcoded a value for that variable.

  5. #5
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Error Handling

    Ok, forgive me because this is the first error handling code I've put together.

    I do believe though it is a scenario that can happen even hard coded. For example if fpath has been hardcoded to look in a certain folder, but inadvertently somone changes the folder structure, then the error would occur.

    Many thanks and kind regards

    Chris

  6. #6
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: VBA Error Handling

    No it wouldn't because you aren't actually testing for the existence of the folder there - you're just asking if the variable is "" which it can't be since you just assigned it a value.

  7. #7
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Error Handling

    Ok I understand. Could you possibly tell me then how I may test for this with the error handling.

    Many thanks and kind regards

    Chris

  8. #8
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Error Handling

    HI @romperstomper, I just wanted to let you know that I continued to read up on this and I've put the following together but the 'Error Handler' runs even when the file path is correct.

    Please Login or Register  to view this content.
    Many thanks and kind regards

    Chris
    Last edited by hobbiton73; 04-30-2015 at 01:43 AM.

  9. #9
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: VBA Error Handling

    You should always have an Exit Sub line before your error handler part.

  10. #10
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Error Handling

    Hi @romperstomper, thank you for coming back to me with this. I've continued to work on this and read many posts and tutorials and I've put together the following:

    Please Login or Register  to view this content.
    The code correctly exits when the directory doesn't exist and runs through the whole script when it does.

    But I do have a couple of issues:

    When the directory doesn't exist, I still receive the "The list has now been been updated!" message and don't receive the 'Handler' message.

    I just wondered whether you may be able to let me know where I've gone wrong.

    Many thanks and kind regards

    Chris
    Last edited by hobbiton73; 04-30-2015 at 03:25 AM.

  11. #11
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: VBA Error Handling

    Your post does not comply with Rule 8 of our Forum RULES. Do not crosspost your question on multiple forums without including links here to the other threads on other forums.

    Cross-posting is when you post the same question in other forums on the web. The last thing you want to do is waste people's time working on an issue you have already resolved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser) to the cross-post.

    Expect cross-posted questions without a link to be closed and a message will be posted by the moderator explaining why. We are here to help so help us to help you!

    Read this to understand why we ask you to do this, and then please edit your first post to include links to any and all cross-posts in any other forums (not just this site).

  12. #12
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Error Handling

    @Kyle123, my sincere apologies.

    Post amended.

    Many thanks and kind regards

    Chris

  13. #13
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Error Handling

    Hi, with the help from a work colleague I've having a working solution as shown below.

    Many thanks and kind regards

    Chris

    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)

Similar Threads

  1. Error handling inside error handling
    By grantastley in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-06-2015, 03:43 AM
  2. [SOLVED] Error Handling: Creating code to display error messages
    By Student1990 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-01-2013, 02:21 PM
  3. Error Handling: Can it report line # where error occurred?
    By PingPing in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-20-2010, 03:47 AM
  4. Error Handling - On Error GoTo doesn't trap error successfully
    By David in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-16-2006, 02:10 PM
  5. [SOLVED] Error handling with a handling routine
    By ben in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-15-2005, 11:06 AM

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