Forum Discussion
Password in Excel only for Save
In Excel, you cannot set a password to prompt for saving without going through a password-protected workbook, which also provides read-only access. However, you can implement a workaround using VBA to accomplish your goal. The idea is to use a macro to prompt for a password when you try to save the workbook, but allow free access for reading and editing.
Here is a step-by-step guide on how to set up such a system:
- Open your Excel workbook.
- Press ALT + F11 to open the Visual Basic for Applications (VBA) editor.
- In the VBA editor, right-click on the workbook name in the Project Explorer window and select "Insert" > "Module" to insert a new module.
- Copy and paste the following VBA code into the module:
Vba code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim Password As String
Password = InputBox("Enter the save password:")
' Replace "YourPassword" with your desired password
If Password <> "YourPassword" Then
MsgBox "Incorrect password. Changes will not be saved.", vbExclamation
Cancel = True
End If
End Sub
5. Modify the code by replacing "YourPassword" with your actual desired save password.
6. Save the workbook as a macro-enabled workbook by selecting "File" > "Save As" and choosing the file type "Excel Macro-Enabled Workbook (*.xlsm)."
7. Close the VBA editor.
Now, whenever you try to save the workbook, Excel will prompt for the save password. If you enter the correct password, it will allow you to save. If you enter the incorrect password or click Cancel, the changes will not be saved.
Please be aware that this method relies on security through obscurity. Anyone with access to the VBA editor can view the password. So, it's not a foolproof method, but it can help prevent accidental saves.The text and steps were edited with the help of AI.
My answers are voluntary and without guarantee!
Hope this will help you.
Was the answer useful? Mark as best response and Like it!
This will help all forum participants.
Thank you for your nice answer.
I try your VBA code and it is not working : I well open the document with "Activate the content" option for Macro.
I opened the VBA editor, copy paste you code, replace the password :
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim Password As String
Password = InputBox("Enter the save password:")
If Password <> "12345" Then
MsgBox "Incorrect password. Changes will not be saved.", vbExclamation
Cancel = True
End If
End Sub
Close the VBA editor, save the file macro enable file.
When I open again the document, I try to modify something and when I save the file, it didn't open a box where we can put the password (here 12345 as an example).
As I am not very good at VBA, could you please help ?
Thank you again,
Camille