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

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

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