Excel VBA: Save and Overwrite Without Prompt
In this article
To save an Excel workbook and overwrite the existing file without the "A file named ... already exists" prompt, set the workbook's Saved property to True before calling SaveAs with the same file name, or use Application.DisplayAlerts = False around the save and restore it afterwards. The first method is safer because it does not suppress unrelated alerts. The exact code depends on whether you are saving the active workbook, a specific workbook, or a copy.
What You'll Need
- Microsoft Excel with macros enabled (the .xlsm format if you want to keep your VBA code).
- Access to the Visual Basic Editor: press Alt+F11 on Windows, or use the Developer tab > Visual Basic. On Mac, use Tools > Macro > Visual Basic Editor.
- The full folder path where the file should be saved. You can get it by right-clicking the file in File Explorer (Windows) or Finder (Mac) and choosing Properties or Get Info.
- Basic familiarity with adding a module and running a macro. No add-ins are required.
Step-by-Step
- Open the Visual Basic Editor. Press Alt+F11 (Windows) or go to Developer > Visual Basic (Mac: Tools > Macro > Visual Basic Editor). If the Developer tab is not visible, enable it in File > Options > Customize Ribbon (Windows) or Excel > Preferences > Ribbon & Toolbar (Mac). Menu labels can differ slightly by Excel version and platform.
- Insert a standard module. In the editor, choose Insert > Module. This is where your macro will live. Do not paste code into the ThisWorkbook or a sheet module unless you specifically want event-driven code.
- Write the save procedure using the Saved property. The safest pattern is to mark the workbook as already saved before calling SaveAs, so Excel does not treat the existing file as a conflict. Example:
Replace the path with your own. UseSub SaveOverwrite() Dim wb As Workbook Set wb = ThisWorkbook wb.Saved = True wb.SaveAs Filename:="C:\Reports\Monthly.xlsx", FileFormat:=xlOpenXMLWorkbook End SubxlOpenXMLWorkbookfor .xlsx,xlOpenXMLWorkbookMacroEnabledfor .xlsm, orxlExcel8for .xls. If you omit FileFormat, Excel may prompt about the format. - Alternatively, suppress alerts temporarily. If you prefer the DisplayAlerts approach, wrap the save and always restore the setting:
TheSub SaveOverwriteAlerts() Application.DisplayAlerts = False On Error GoTo CleanUp ThisWorkbook.SaveAs Filename:="C:\Reports\Monthly.xlsx", FileFormat:=xlOpenXMLWorkbook CleanUp: Application.DisplayAlerts = True End SubOn Error GoTo CleanUpline ensures alerts are turned back on even if the save fails. Never leave DisplayAlerts set to False after your macro ends. - Save to the same file name. The overwrite prompt appears when the target file already exists. If you use a new name each time (for example with a date stamp), no prompt appears. To overwrite deliberately, keep the same path and file name in the Filename argument.
- Run the macro. Press F5 in the editor, or go back to Excel and use Developer > Macros, select your procedure, and click Run. If the file is open in another Excel window, close it first; Windows will otherwise block the save with a sharing violation.
- Verify the result. Check the file's modified date in File Explorer or Finder. Open it to confirm the content is the version you expected. If the workbook is read-only, the save will fail; check the file properties and clear the read-only attribute.
Common Mistakes
- Leaving DisplayAlerts off. If your macro errors before the restore line, Excel keeps suppressing alerts for the rest of the session. Always use error handling or the Saved property method.
- Using SaveAs with no FileFormat. Excel may show a compatibility prompt. Specify the format constant explicitly.
- Saving a different workbook than you think.
ThisWorkbookrefers to the workbook containing the macro;ActiveWorkbookrefers to whatever is in front. If your macro runs from an add-in or personal workbook, ThisWorkbook is not the file you want. Use an explicit workbook reference. - Forgetting the file extension. If you type a path without .xlsx or .xlsm, Excel may append its default or prompt about the format.
- Saving to a folder that does not exist. SaveAs fails if the folder path is missing. Create the folder first with
MkDiror save to an existing location. - Assuming the prompt is about overwriting when it is about read-only or sharing. A file open in another program produces a different error. Close the other copy first.
Tips
- Prefer the
Saved = Truemethod for routine overwrites. It avoids the prompt without hiding genuine warnings such as a full disk or a missing folder. - Build the path with
ThisWorkbook.Pathwhen saving alongside the current file, so the macro keeps working if the folder moves:Filename:=ThisWorkbook.Path & "\Monthly.xlsx". - Use
Application.ScreenUpdating = Falsearound long save routines to reduce flicker, and restore it afterwards. - If you need a backup before overwriting, use
FileCopyor save a copy with a timestamped name first, then overwrite the main file. - Test macros on a copy of the workbook, not the original, until you are confident the path and format are correct.
Troubleshooting
If the prompt still appears, check that you are not calling SaveAs twice or saving a workbook that was opened read-only. Confirm the path in the Filename argument matches the existing file exactly, including extension and folder. If you get a "method SaveAs of object Workbook failed" error, the most common causes are a missing folder, a file open elsewhere, or an invalid FileFormat constant for the extension you chose. If the macro appears to run but nothing changes, check whether the workbook was saved to a different location than you expected; use MsgBox ThisWorkbook.FullName to display the actual path. If DisplayAlerts was left off from an earlier run, close and reopen Excel to reset it. If the file is on a network share, confirm you have write permission; a permissions error looks like a save failure.
Frequently Asked Questions
Why does SaveAs prompt even when I set DisplayAlerts to False?
DisplayAlerts does not suppress every prompt, and if an error occurs before the restore line, the setting may not be active when the save runs. Use the Saved = True method or wrap the save in error handling that restores DisplayAlerts.
Can I overwrite a file that is open in another Excel window?
No. Windows locks open files, so the save fails with a sharing violation. Close the other copy first, or save to a different file name.
Does this work on Excel for Mac?
Yes, the same VBA properties and methods apply. Paths use Mac-style separators in some contexts, but VBA accepts the colon-separated HFS path or a POSIX path depending on version. Use ThisWorkbook.Path to avoid hard-coding separators.
How do I save without changing the file format?
Omit the FileFormat argument and keep the existing extension, or specify the matching constant. If the workbook contains macros, use xlOpenXMLWorkbookMacroEnabled for .xlsm.
Will this overwrite the file silently even if it is read-only?
No. A read-only file cannot be overwritten by SaveAs; the call fails. Clear the read-only attribute in the file properties first, or save under a new name.
Can I avoid the prompt without VBA?
Excel's normal Save command overwrites the current file without a prompt. The prompt appears mainly for Save As to an existing name. If you are not using a macro, just use Ctrl+S (Windows) or Command+S (Mac).