Excel VBA Save As Read Only: Step-by-Step Guide
In this article
Saving a workbook as read-only with Excel VBA means writing a macro that calls the SaveAs method with the ReadOnlyRecommended argument set to True. That does not lock the file with a password; it sets a flag so Excel asks anyone opening the file whether they want to open it read-only. The shortest working line is ActiveWorkbook.SaveAs Filename:="C:\Reports\Q1.xlsx", ReadOnlyRecommended:=True. You can also save to a path that already has the read-only file attribute set, or mark the file read-only after saving, but the VBA flag is the cleanest approach.
What You'll Need
- Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > check Developer, or Excel > Preferences > Ribbon & Toolbar on Mac).
- A workbook you are allowed to modify and save.
- A destination folder you can write to. Avoid OneDrive or SharePoint sync folders while testing, because sync can interfere with the read-only flag.
- Basic familiarity with the VBA editor (Alt+F11 on Windows, Option+F11 on Mac).
Menu labels can differ slightly by Excel version, platform and region, so if a name does not match exactly, look for the closest equivalent.
Step-by-Step
- Open the workbook you want to save. Make your edits first. The macro saves the current state of the file, so any unsaved changes are included.
- Open the VBA editor. Press Alt+F11 (Windows) or Option+F11 (Mac). In the Project pane, right-click your workbook and choose Insert > Module. A blank module appears.
- Paste the macro. Use this code, changing the path and file name to a real folder on your machine:
On Mac, use a path likeSub SaveAsReadOnly() Dim savePath As String savePath = "C:\Reports\Q1_ReadOnly.xlsx" ActiveWorkbook.SaveAs Filename:=savePath, _ FileFormat:=xlOpenXMLWorkbook, _ ReadOnlyRecommended:=True End Sub"/Users/YourName/Documents/Q1_ReadOnly.xlsx". If you omitFileFormat, Excel picks a default based on the extension, which is usually fine but can trigger a compatibility prompt. - Run the macro. Place the cursor inside the subroutine and press F5, or go back to Excel and use Developer > Macros, select
SaveAsReadOnly, and click Run. - Confirm the read-only prompt. Close the workbook, then reopen the saved file. Excel should show a message asking whether to open it read-only. Click Yes to open it in read-only mode, or No to open it normally and edit it.
- Verify the flag. In File Explorer (Windows), right-click the file > Properties. The Read-only checkbox reflects the file attribute, not the
ReadOnlyRecommendedflag, so do not rely on it alone. Reopening the file and seeing the prompt is the real test. - Optionally set the file attribute too. If you want the file to be read-only at the file-system level, add a line after saving:
SetAttr savePath, vbReadOnly. This makes the file read-only for everyone until the attribute is cleared, which is stronger than the recommendation flag.
Common Mistakes
- Confusing the recommendation flag with password protection.
ReadOnlyRecommended:=Trueonly prompts the user; anyone can click No and edit the file. For real protection, usePassword:="..."andWriteResPassword:="..."in the sameSaveAscall. - Saving over the open workbook. If the destination path is the same file you have open, Excel may raise an error or save silently without the flag. Save to a new file name first.
- Using a path that does not exist.
SaveAsdoes not create folders. Create the destination folder before running the macro, or the macro fails with a run-time error. - Forgetting the file extension. If you supply a name without
.xlsx, Excel may append a default format or prompt about compatibility. - Expecting the flag to survive a re-save. If someone opens the file, clicks No, edits it and saves, the recommendation flag is typically preserved only if they save with the same option; otherwise it can be lost. Treat the flag as a nudge, not a lock.
- Running the macro on a protected sheet. Sheet protection does not block
SaveAs, but workbook structure protection can. Unprotect the workbook first if the macro errors.
Tips
- Use
ThisWorkbookinstead ofActiveWorkbookif the macro lives in the workbook being saved, so the wrong file is never saved by accident. - Build the path with
Environ("USERPROFILE")orThisWorkbook.Pathso the macro works on other machines. - Add
DisplayAlerts = Falsebefore saving and restore it toTrueafterwards if you want to suppress the overwrite prompt. Remember to restore it, or later prompts will be hidden. - Combine the recommendation flag with a write password when the goal is to stop casual edits but still let authorised users save changes.
- Test with a copy of the workbook, not the original, until you are confident the macro saves where you expect.
Troubleshooting
If the macro runs but no read-only prompt appears when you reopen the file, check that you saved to a new path and that the file was not already open in another Excel window. Close all Excel instances, reopen the saved file, and look again. If the prompt still does not appear, the file may have been saved by a different method; re-run the macro and confirm the ReadOnlyRecommended argument is spelled correctly.
If you get a run-time error 1004, the most common causes are a missing folder, an invalid path, a file that is already open, or a protected workbook. Create the folder, close other copies of the file, and unprotect the workbook structure. On Mac, check that the path uses forward slashes and that Excel has permission to write to the folder.
If the file opens read-only every time without asking, the file attribute may be set, or the file may be in a synced folder that marks it read-only. Clear the attribute with SetAttr savePath, vbNormal and move the file out of the synced folder if needed.
If the macro saves the wrong workbook, replace ActiveWorkbook with ThisWorkbook or with an explicit Workbooks("Name.xlsx") reference.
Frequently Asked Questions
Does ReadOnlyRecommended actually prevent editing?
No. It only prompts the person opening the file. They can choose No and edit normally. Use a write password for real protection.
Can I save as read-only without VBA?
Yes. Use File > Save As, click the Tools dropdown next to Save, choose General Options, and tick Read-only recommended. The VBA method just automates the same flag.
Will the read-only flag work on OneDrive or SharePoint?
The recommendation flag is stored in the file, but sync and co-authoring can change how the prompt appears. Test on the actual location before relying on it.
How do I remove the read-only recommendation later?
Re-save the file with ReadOnlyRecommended:=False, or use File > Save As > Tools > General Options and clear the checkbox.
Can I set a read-only password instead?
Yes. Add WriteResPassword:="yourpassword" to the SaveAs call. Users can open the file but need the password to save changes.
Why does the macro fail on a network drive?
Usually permissions or a mapped drive letter that is not available. Use the full UNC path (\\server\share\folder\file.xlsx) and confirm you have write access.