+ Reply to Thread
Results 1 to 7 of 7

Excel VBA Save As (No Overwrite / Clear Macros)

  1. #1
    Registered User
    Join Date
    09-27-2015
    Location
    AZ
    MS-Off Ver
    2010
    Posts
    23

    Excel VBA Save As (No Overwrite / Clear Macros)

    Have a question on Excel VBA save as functionality. The following basically grabs the filename entered by the user from a cell in the workbook and saves the workbook as a non-macro enabled workbook (xlsx).

    Please Login or Register  to view this content.
    It works, but was wondering what is the best way modify to account for the issues below:

    1. I do not want the user to be able to overwrite existing files. So if the file exists I would like the to be prompted to change the file name.

    2. The Save As xlsx will drop all the active macros. But it does not do this till the user closes the file. The problem is I have a "Workbook_BeforeClose" sub within the original workbook. This is only supposed to run on the original document. But it is running when the new file is closed. Is there a way to clear the active macros after the save as?

  2. #2
    Forum Contributor
    Join Date
    08-18-2017
    Location
    india
    MS-Off Ver
    2010
    Posts
    490

    Re: Excel VBA Save As (No Overwrite / Clear Macros)

    for issue no. 1 - i guess you can save it as read only file

  3. #3
    Registered User
    Join Date
    09-27-2015
    Location
    AZ
    MS-Off Ver
    2010
    Posts
    23

    Re: Excel VBA Save As (No Overwrite / Clear Macros)

    Figured out the answer to number 2. I placed an if statement in the "Workbook_BeforeClose" sub to check if the file name matches the template. If not, it bypasses the function that is not needed.

    Please Login or Register  to view this content.
    For number 1, "Application.DisplayAlerts = False" is bypassing the prompt for saving without macros as intended. But it is also bypassing the overwrite existing file prompt. Is there maybe a different way to get around the macro prompt? Or possibly an if statement that checks "Filename" before it proceeds with the save?

    Appreciate your help on this!

  4. #4
    Registered User
    Join Date
    09-27-2015
    Location
    AZ
    MS-Off Ver
    2010
    Posts
    23

    Re: Excel VBA Save As (No Overwrite / Clear Macros)

    Figured it out. The answer was to use FSO to check for the file name. I have placed the code below in case anyone is looking for a similar solution.

    Please Login or Register  to view this content.
    If anyone has a way I could improve upon either solution I would appreciate the feedback.

  5. #5
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Excel VBA Save As (No Overwrite / Clear Macros)

    Hi there,

    Here's another possible approach - it uses Dir rather than FileSystemObject to determine whether or not the file already exists:

    Please Login or Register  to view this content.
    The highlighted values may be altered to suit your requirements.


    Hope this helps.

    Regards,

    Greg M

  6. #6
    Registered User
    Join Date
    09-27-2015
    Location
    AZ
    MS-Off Ver
    2010
    Posts
    23

    Re: Excel VBA Save As (No Overwrite / Clear Macros)

    Thanks for your help!

    One more question I ran into on this. The users will be on a local network or connected to a VPN. When testing this I tried to run while not connected to the VPN. It freezes up excel.

    Is there a way to display an error message if the folder is unreachable?

  7. #7
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Excel VBA Save As (No Overwrite / Clear Macros)

    Hi again,

    The following code will generate an error message if the workbook cannot be saved:

    Please Login or Register  to view this content.

    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

+ 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. Save As PDF And (please do not) Overwrite Existing
    By cangokturk in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-06-2016, 02:30 PM
  2. Replies: 1
    Last Post: 09-05-2013, 02:39 PM
  3. [SOLVED] Macro with "save as" --- Need excel to overwrite a file without a prompt to overwrite
    By tsmith1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-21-2012, 10:54 AM
  4. [SOLVED] Get Macros To Overwrite Total On Same Cell
    By NewbieOfVBA in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-26-2012, 06:44 AM
  5. Overwrite Prompt with Save As
    By Chip in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-20-2005, 04:05 PM
  6. [SOLVED] Excel prompts me to save a copy or overwrite file.
    By Vinny in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 05-20-2005, 11:06 AM
  7. [SOLVED] Save as Overwrite prompt
    By Steph in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-25-2005, 09:06 PM

Tags for this Thread

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