VBA Script to automatically apply Password to Workbook when closing, prompt user for PW when opening and setting to read-only if input incorrect.

Funkk

New Member
Joined
Aug 4, 2022
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
Hello Everyone

Been scouring these forums for a few days before deciding to make my own account... There's been many topics that come extremely close to what I'm looking for, yet none have been the exact same to my knowledge.

So, I'd like for the entire workbook to be Password protected upon closing it, so that every user who opens it must first input the password.
It should preferably pop up in an inputBox. If the user gets the PW right, he can modify the file to his liking.
If he gets it wrong or hits "cancel", he should still be able to look at and copy data from any cell he wants, but he cannot add anything new.

Could you guys maybe help me with that?


What I have so far:

VBA Code:
Option Explicit
Private Sub Workbook_Open()
Dim xSheet As Worksheet
Dim xPsw   As String
Dim answer As String
xPsw = "PASSWORD"
answer = InputBox("Input password if you want to unprotect" & vbLf & "all your sheets then press OK.")
If answer = xPsw Then
For Each xSheet In Worksheets
xSheet.Unprotect xPsw
Next
MsgBox "Done!"
End If
End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top