VBA & Macro Problems

Require a Password Before Saving an Excel Workbook with VBA

Anum MushtaqAnum Mushtaq Nov 12, 2026 869 views

Question details

The user needs to require a password when saving changes to an Excel workbook while allowing users to open and edit it without a password.

Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
A workbook should remain freely accessible for viewing and editing, but saving changes should require a specific password.
Observed behavior
Excel has no built-in setting that prompts for a password only when saving changes; a Workbook_BeforeSave VBA event is needed as a workaround.
Before you start

Confirm that macros are allowed in the workbook and plan to save the file in the macro-enabled .xlsm format. Decide whether users should be able to inspect or modify the VBA project, because this method is a deterrent rather than strong security.

Solution 1Recommended

Use the Workbook_BeforeSave event to require a save password

Use this VBA workaround when users may open and edit the workbook freely but must enter a password before Excel saves changes.

Excel does not provide a standard password setting for saving only. The Workbook_BeforeSave event can display a prompt and cancel the save when the entered value is incorrect.

This method does not provide strong protection. Users with access to the VBA project may be able to view, disable, or remove the password check.

1
Open the ThisWorkbook module

In Excel, press Alt+F11 to open the Visual Basic Editor. In the Project Explorer, expand the workbook, then double-click ThisWorkbook.

2
Add the BeforeSave event code

Paste this code into the ThisWorkbook module: Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)\n Dim password As String\n password = InputBox("Enter the save password:")\n If password <> "YourPassword" Then\n MsgBox "Incorrect password. The changes will not be saved.", vbExclamation\n Cancel = True\n End If\nEnd Sub

3
Replace the sample password

In the VBA code, replace YourPassword with the password you want to require. Keep the password value protected if the workbook contains sensitive information.

4
Save as a macro-enabled workbook

Close the Visual Basic Editor, select File > Save As, choose Excel Macro-Enabled Workbook (*.xlsm), and save the file.

5
Test the save prompt

Edit a cell and select File > Save. Enter the correct password to save the changes, or enter another value and confirm that Excel cancels the save.

Security limitation: The VBA prompt is a deterrent and not a secure access-control mechanism. Protect the VBA project separately and use Excel's built-in file encryption when strong confidentiality is required.
Free Microsoft Office alternative

Edit Excel Workbooks with a Free, Lightweight Office Alternative

WPS Office provides a free, lightweight spreadsheet editor with a familiar interface and broad Microsoft Office format compatibility, making migration from Excel straightforward. However, the Workbook_BeforeSave VBA password behavior described above is an Excel macro workaround and may not run identically in WPS Office.

Edit and manage spreadsheets in a familiar, lightweight interface.Open and save common Microsoft Excel .xlsx and .xls formats.Support common Office document workflows with easy migration from Excel.Review workbook content without requiring a full Microsoft Office installation.
microsoft office alternative - wps office

Frequently Asked Questions

Can Excel require a password only when a workbook is saved?

Excel has no standard setting for this exact behavior. Use the Workbook_BeforeSave event in the ThisWorkbook VBA module to prompt for a password and set Cancel = True when the password is incorrect.

Where should the save-password VBA code be placed?

Press Alt+F11, locate the workbook in the Project Explorer, and open the ThisWorkbook module. The Workbook_BeforeSave procedure must be placed there rather than in a regular module.

What file type is required for the BeforeSave macro?

Save the workbook as an Excel Macro-Enabled Workbook (*.xlsm). Saving it as .xlsx removes the VBA project and prevents the save prompt from running.

Will the VBA save password provide strong security?

No. It only deters ordinary saves. Anyone who can access or modify the VBA project may be able to inspect, disable, or remove the password check. Use file encryption for stronger protection.

What happens when the wrong password is entered?

The macro displays an error message and sets the BeforeSave event's Cancel argument to True, so Excel cancels the save and keeps the changes unsaved.