+ Reply to Thread
Results 1 to 11 of 11

VBA to overwrite existing file without warning

  1. #1
    Forum Contributor myobreportguru's Avatar
    Join Date
    09-18-2012
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2016
    Posts
    231

    VBA to overwrite existing file without warning

    Hi

    I have some vba which saves mutiple files however if there's a file already there with the same name the code stops with an error. It doesn't matter if these files are overwritten. Here is the code at the moment.

    wb.Close savechanges:=False
    destWb.SaveAs "C:\xxxxxx
    destWb.Close False

    Many thanks in advance.

    Myob Report Guru

  2. #2
    Valued Forum Contributor
    Join Date
    03-24-2020
    Location
    Thailand
    MS-Off Ver
    Office 2024
    Posts
    996

    Re: VBA to overwrite existing file without warning

    Hi there,


    Depends what you mean with an 'error'. If it simply stops and ask if you want to overwrite, then try by adding this line of code at the start:

    Please Login or Register  to view this content.
    This should stop excel to show alerts, then at the end switch alerts back on by:

    Please Login or Register  to view this content.

    If it is a different 'error', please be more specific; error code, message.
    Last edited by ORoos; 02-17-2022 at 08:51 PM.
    If your Question is answered; please mark it SOLVED. If you are happy with a member's solution, say 'Thanks' and click the 'Star' to Add Reputation.

  3. #3
    Forum Contributor myobreportguru's Avatar
    Join Date
    09-18-2012
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2016
    Posts
    231

    Re: VBA to overwrite existing file without warning

    Sorry it stops with a vba error code which says something like 'file already exists' along with the error code 58. When I pressed debug it highlighted the code which I put above. Hope this helps.

  4. #4
    Forum Contributor
    Join Date
    01-02-2020
    Location
    Idaho, USA
    MS-Off Ver
    365
    Posts
    274

    Re: VBA to overwrite existing file without warning

    Delete the file first if it exists:

    wb.Close savechanges:=False
    If dir$("C:\xxxxx") <> "" then kill "C:\xxxxx"
    destWb.SaveAs "C:\xxxxxx
    destWb.Close False

  5. #5
    Forum Contributor myobreportguru's Avatar
    Join Date
    09-18-2012
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2016
    Posts
    231

    Re: VBA to overwrite existing file without warning

    I'm getting a compile error when I tried to add your code. I've put more info in that might help. Thanks for your help.

    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    01-02-2020
    Location
    Idaho, USA
    MS-Off Ver
    365
    Posts
    274

    Re: VBA to overwrite existing file without warning

    The idea is to delete the exact file you are trying to save over the top of. That way there is no error of it already existing.
    Just check to see if the file exists using Dir$, then use the Kill command to delete it if it is there. So in this example, I use Dir$ to see if the file exists. If it does, use Kill to delete it. Then when you do the SaveAs there is no error because the file will not exist since it's been deleted. i.e...

    wb.Close savechanges:=False
    If Dir$("C:\Users\xxxx\Sales Reports\" & CreateFileName(destWks.Range("a4").Value)) <> "" Then Kill "C:\Users\xxxx\Sales Reports\" & CreateFileName(destWks.Range("a4").Value)
    destWb.SaveAs "C:\Users\xxxx\Sales Reports\" & CreateFileName(destWks.Range("a4").Value)
    destWb.Close False

  7. #7
    Forum Contributor myobreportguru's Avatar
    Join Date
    09-18-2012
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2016
    Posts
    231

    Re: VBA to overwrite existing file without warning

    Thanks for your reply. It looks like that problem is fixed but now I have another one. See below. The problem is on these 2 lines -
    Name folderPath & "\" & fileName As _
    "C:\Users\xxxx\Sales Report Raw Data Actioned\" & fileName

    Please Login or Register  to view this content.

  8. #8
    Forum Contributor myobreportguru's Avatar
    Join Date
    09-18-2012
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2016
    Posts
    231

    Re: VBA to overwrite existing file without warning

    I think we have to delete and replace any duplicates in folder as well. This is the address where the raw data file gets moved too. Thanks again.

  9. #9
    Forum Contributor
    Join Date
    01-02-2020
    Location
    Idaho, USA
    MS-Off Ver
    365
    Posts
    274

    Re: VBA to overwrite existing file without warning

    Your problem is starting to get a little vague. It looks like you are using a couple of custom functions that I have no idea what they are doing. I do know that the file you are trying to rename can not already exist in the same folder, so you could try this. Same as before, this will delete the file if it already exists before moving or renaming a file to that name.

    Please Login or Register  to view this content.

  10. #10
    Forum Contributor myobreportguru's Avatar
    Join Date
    09-18-2012
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2016
    Posts
    231

    Re: VBA to overwrite existing file without warning

    Thank you so much it now works without any problems. Just to put you in the picture for this project I'm sent sales figures for multiple stores all in one spreadsheet. I get one for every week that covers 7 days at all stores. Another colleague wrote the original code but now he's tied up with other work and I was under pressure to get this sorted for my bookkeeping staff. The whole project is to create new spreadsheets with each store on different tabs for each week and then move them to another folder. The original raw data files are moved also to another folder. The problem came when raw data files covered previous periods that we had already processed so hence we had to delete the older version of raw data files and our completed sales files. Thanks again for all your help. It has been really appreciated.

  11. #11
    Forum Contributor
    Join Date
    01-02-2020
    Location
    Idaho, USA
    MS-Off Ver
    365
    Posts
    274

    Re: VBA to overwrite existing file without warning

    You're welcome! I'm glad you got it working!

+ 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. VBA: Print to PDF and Overwrite Existing file
    By pascawali in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-23-2020, 01:09 AM
  2. [SOLVED] Overwrite existing file
    By dekueb in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-17-2018, 03:12 PM
  3. overwrite existing file automaticly
    By hakunaberlin in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-24-2016, 02:23 PM
  4. Need to Overwrite existing XML file - Error 58
    By VBABegin in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-27-2016, 11:11 AM
  5. Save As-Overwrite existing File
    By loulorisgomez in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-03-2012, 07:24 AM
  6. .saveas issue. Overwrite existing file?
    By nig_44 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-29-2012, 03:09 PM
  7. Export And Overwrite Existing file
    By okelly in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-20-2007, 11:19 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