+ Reply to Thread
Results 1 to 19 of 19

Error Handling - No or Cancel throws up Error

  1. #1
    Forum Contributor
    Join Date
    09-22-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Office 2019
    Posts
    309

    Error Handling - No or Cancel throws up Error

    Hi

    On saving a workbook to a folder the expected dialogue box "A file by this name already exist. Do you want to overwrite it?" comes up. The answer is usually "Yes" and everything moves ahead sweetly. However, if the answer is "No" or you click "Cancel" it throws up an error.

    I've tried to use an error handling code I found on the Internet and have fiddled with it, but can't get it to work for me.

    The relevant bit of my code with the error handler in Brown is as follows:

    Please Login or Register  to view this content.
    I appreciate that this is the subject of many queries and I've read many of them; attempted to put an error handler in my code but without success and now need some help please.

    Many thanks in advance

    Frankie

  2. #2
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,516

    Re: Error Handling - No or Cancel throws up Error

    Replace or add as required and try it.

    Please Login or Register  to view this content.

  3. #3
    Forum Contributor
    Join Date
    09-22-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Office 2019
    Posts
    309

    Re: Error Handling - No or Cancel throws up Error

    Hi Jolivanes

    Sorry, It's taken a bit of time to get back on this. The Code works well thanks, but it leaves the workbook that has been produced open. I did a small modification and added wb.Close SaveChanges:=False after the "I had enough" message but that message came up when I actually clicked "OK" on the original "A file already exists……" message.

    How would I get the Code to close the produced workbook after the error message box has been closed please?

  4. #4
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,516

    Re: Error Handling - No or Cancel throws up Error

    Show us your whole code please.

  5. #5
    Forum Contributor
    Join Date
    09-22-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Office 2019
    Posts
    309

    Re: Error Handling - No or Cancel throws up Error

    Please Login or Register  to view this content.

  6. #6
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,516

    Re: Error Handling - No or Cancel throws up Error

    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    09-22-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Office 2019
    Posts
    309

    Re: Error Handling - No or Cancel throws up Error

    Nearly there! This works OK, but takes away the option to overwrite the existing sheet as it may well be that the sheet being produced has had some changes to it that need to be save. The original issue is that if a file of the same name exists, Excel (?) puts up a Yes - No - Cancel choice for saving the sheet. If you click "Yes" everything is good and the sheet in the folder is overwritten. However, if you press "No" or "Cancel" it threw up an error (1004 Method 'Save As' of object '_Workbook' failed). I'm looking to be able to either click "Yes" and save / overwrite the sheet, or click on "No" or "Cancel" close the sheet produced without saving and end the Code.

    Thanks for the continued responses. Very much appreciated!

  8. #8
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,516

    Re: Error Handling - No or Cancel throws up Error

    It looks like we're talking of different things.
    The code from Post #6 should only save a sheet as a workbook.
    I see now that you have the line
    Please Login or Register  to view this content.
    twice.
    The code from Post #6 should replace below the 2nd time it appears in your supplied code from Post #5.

    Is that not the problem you're talking about?

    It'd bed time now so it'll be a while.

  9. #9
    Forum Contributor
    Join Date
    09-22-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Office 2019
    Posts
    309

    Re: Error Handling - No or Cancel throws up Error

    Ha! Did an Edit Post and it's wiped my response!!
    Last edited by Frankie_The_Flyer; 10-18-2019 at 03:17 AM.

  10. #10
    Forum Contributor
    Join Date
    09-22-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Office 2019
    Posts
    309

    Re: Error Handling - No or Cancel throws up Error

    I added the code under the second "If eCell….." which is commented out (a result of previous changes!).

    If there was a file of the same name already in the folder the original code at post 1 would bring up the "Yes" "No" "Cancel" dialogue box after the code lines


    If I clicked "Yes" everything was fine and the sheet saved overwriting the current one. Clicking "No" or "Cancel" would cause the cause the code fall over (End / Debug message)
    I fiddled around adding the code in brown (in post 1) but failed quite badly to achieve anything!

    What we may aim at is getting the message box to say something along the lines of "The file:" & vbLf & fName & vbLf & "already exists. Do you want to overwrite it?" If we then had a "Yes" and a "No" button that saved / overwrote the file on "Yes" or does what the error handling code in post 6 does if you click on "No", we should be about right.

    If you click "Yes" the next message box should then say "The file:" & vbLf & fName & vbLf & " has been saved". (It should say this when the sheet is saved even if there isn't file to overwrite too). Having a message that says "The file has not been saved" after clicking "No" or "Cancel" would be great too! (Some of the potential users are not very computer savvy so the more info I can give them the better!)

    Sleep well. I should be doing the same in a couple of hours (just on Friday night rather than Thursday!) but making excel do magic things with this coding lark keeps me awake quite often!!

    Frankie

  11. #11
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Error Handling - No or Cancel throws up Error

    How about this one ?

    If you click No in the msgbox code is aborted.

    You have to delete the old file on your HD before you can save the new file with the same name. The Kill command takes care of that.

    Please Login or Register  to view this content.
    Be sure to test this on some copies of your files.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  12. #12
    Forum Contributor
    Join Date
    09-22-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Office 2019
    Posts
    309

    Re: Error Handling - No or Cancel throws up Error

    After the code lines
    Please Login or Register  to view this content.
    Didn't want to risk having the post wiped again by editing it!!!

  13. #13
    Forum Contributor
    Join Date
    09-22-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Office 2019
    Posts
    309

    Re: Error Handling - No or Cancel throws up Error

    Hi Bakerman2

    Thanks for the input, however this brings up a Run Time error 53 "File not Found" at Kill fName. I think the file has already been deleted at the message box stage two lines up, although the message box didn't come up before the Run Time error appeared.

    Frankie
    Last edited by Frankie_The_Flyer; 10-18-2019 at 03:40 AM.

  14. #14
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Error Handling - No or Cancel throws up Error

    Do you save your copied files as xlsm or xlsx ?

  15. #15
    Forum Contributor
    Join Date
    09-22-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Office 2019
    Posts
    309

    Re: Error Handling - No or Cancel throws up Error

    Xlsx.
    xlsm files don't seem to work from shared folders which is where I have to save them

  16. #16
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Error Handling - No or Cancel throws up Error

    On second thought.

    Please Login or Register  to view this content.
    Last edited by bakerman2; 10-18-2019 at 08:32 PM. Reason: Updated code

  17. #17
    Forum Contributor
    Join Date
    09-22-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Office 2019
    Posts
    309

    Re: Error Handling - No or Cancel throws up Error

    OK. Here's where I've go to so far.

    I've had a play with the suggested code changes and have managed to get a Yes / No MsgBox up.

    If No is selected the vbNo tells the operator that the file hasn't been saved and exits the Sub which is fine. (A win!!!)

    The "If vbYes Then" is meant to do everything the original code did (produce the sheet etc) but do wb.fname.Save rather than wb.fname.SaveAs because I think the SaveAs is what brings up another alert for the file already existing.

    However, If Yes is selected it appears the "If vbYes" is ignored and the code moves down to "Else" and then faults with a "Run-time error 91 - Object variable or With block variable not set" at wb.SaveAs fname. (Reverting to the Path \ filename makes no difference)

    Here's the revised code
    Please Login or Register  to view this content.
    Can anyone help with why it appears to skip over the "If vbYes Then"
    And
    Why it throws up the Run-time error after "Else"

    We may have solved it then!!

    Cheers

    Frankie
    Last edited by Frankie_The_Flyer; 10-18-2019 at 11:34 PM.

  18. #18
    Forum Contributor
    Join Date
    09-22-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Office 2019
    Posts
    309

    Re: Error Handling - No or Cancel throws up Error

    HA! (again!) I spent all morning working on my version only to find a second page to the thread and the response above (which I've added a few message boxes to show the file saved or not) and which works perfectly!!!

    Thank you Bakerman2 and Jolivanes for your excellent responses and for getting me moving again!

    Very grateful to you both.

    Frankie

  19. #19
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Error Handling - No or Cancel throws up Error

    Glad to help and thanks for rep+.

+ 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. [SOLVED] Odd Error Handling Behaviour; goes to error handler even when no error?
    By kalikj in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-04-2018, 10:04 AM
  2. Replies: 2
    Last Post: 02-03-2016, 12:23 PM
  3. RUN TIME ERROR '1004' error handling help needed
    By skop89 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-09-2014, 12:54 PM
  4. [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
  5. Cancel out of Input box for error handling for dates
    By hermithead in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-06-2013, 05:43 AM
  6. 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
  7. 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

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