Forum Discussion
Have macro run to protect file with encrypted password
egillhelga Yes and No. I want to first say that Excel encryption and password protection is ... let's say ... Not Military grade. Second, you are switching between protecting the workbook and protecting a sheet. These are actually 2 different things in Excel. As for the date I don't know if this is based on a date on a sheet or fixed date or what so I will just use a fixed date and you can substitute a sheet reference if needed. Lastly, you still need a trigger to run the macro, so I just assumed on open.
So if you open the macro editor (Alt+F11) and double click 'ThisWorkbook' for your workbook (should show on the left in the project directory) then paste the following:
Private Sub Workbook_Open()
Dim ws As Worksheet
If Now() > DateSerial(2020, 1, 31) Then
ActiveWorkbook.Unprotect "password123"
ActiveWorkbook.Protect "password123"
For Each ws In ActiveWorkbook.Sheets
ws.Protect "password123"
Next
End If
End Sub
and finally you need to right click on VBAProject (Book1) or whatever your workbook name is in the Project directory on the left and select VBAProject Properties... Then in the popup select the 'Protection' tab and set a password (and confirm) so that others can't open this macro editor viewer and see your password.
mtarler Thank you. This runs neatly. But what I needed is not protection of the workbook by utilizing the Review-Protect Workbook function, but rather the function that is available with File-Info-Protect Workbook-Encrypt with Password.
The protection via Review-Protect Workbook, can usually quite easily be bypassed or the password hacked.
Could your macro be changed to have the function be: File-Info-Protect Workbook-Encrypt with Password?