Using VBA: protecting and unprotecting a workbook

chesterbroek

Board Regular
Joined
Jan 25, 2010
Messages
62
hi all,

i'm relatively new to VBA code and would like to protect my workbook when the workbook is opened.

subsequently, i would like the user to be able to unprotect the workbook via clicking a button. this button would first ask the user for a password before unprotecting the workbook.

how can i code this through VBA?

i already built the following VBA code:

upon opening the workbook:

Private Sub Workbook_open()
Application.ScreenUpdating = False
Application.DisplayFullScreen = True
ActiveWindow.DisplayWorkbookTabs = False
Sheets("1. Cover").Activate
Disclaimer.Show
ActiveWorkbook.Protect password:="AS CSM"
Application.ScreenUpdating = True
End Sub

upon pressing the button:

Sub Unprotect_workbook()
Dim psswrd As String
psswrd = InputBox("Please enter password", "Password required")
If psswrd = "" Then Exit Sub
On Error GoTo Error
ActiveWorkbook.Unprotect password:=psswrd
MsgBox "Workbook is unprotected"
Exit Sub
Error:
MsgBox "Incorrect password: workbook could not be unprotected"
Exit Sub
End Sub

this VBA code doesn't seem to function properly though... any help is more than welcome!!!



thanks!

Chester
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Exactly how is your code NOT functioning?? I haven't tested it, but I don't see an obvious error!

lenze
 
Upvote 0
well, the workbook doesn't protect itself. i've locked cells on a specific sheet but i can still access the cells even when the script has run. i think it may have soemthing to do with my password...



cheers!

Chester
 
Upvote 0
OK: Let's step back!! Protecting a workbook does NOT prevent changes in cells on specific sheets!! Protecting a workbook prevents
Adding Sheets
Deleting Sheets
Renaming Sheets
To get the protection you desire, you need to protect EACH sheet
Code:
ActiveSheet.Protect "password"
lenze
 
Upvote 0
aha alright then; makes more sense already... now how can i apply this concept to all sheets within a workbook?



thanks!

Chester
 
Upvote 0
Like this??
Code:
Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In Sheets
ws.Protect "password", UserInterFaceOnly:=True
Next ws
End Sub
This will allow code to run on the protected sheets
HTH
lenze
 
Upvote 0
grand!!! i've now got this and it seems to be working:

Private Sub Workbook_open()
Application.ScreenUpdating = False
Application.DisplayFullScreen = True
ActiveWindow.DisplayWorkbookTabs = False
Sheets("1. Cover").Activate
Disclaimer.Show
ActiveWorkbook.Protect "AS CSM"
For Each Worksheet In ActiveWorkbook.Worksheets
Worksheet.Protect password:="AS CSM"
Next Worksheet
Application.ScreenUpdating = True
End Sub

could you explain the userfaceonly line?



best!

Chester
 
Upvote 0
UserInterFaceOnly sets the protection to a level that allows VBA code to manipulate the sheet, but prevents a User (UserInterFace) from changing the sheet via the keyboard. It is an optional feature. That said, your code is bloated!! All you need is what I posted
Code:
Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In Sheets
ws.Protect "AS CSM", UserInterFaceOnly:=True
Next ws
End Sub
lenze
 
Upvote 0
understand... funny thing now is that i've omitted the code we've been discussing and it STILL protects the worksheets... howcome?!? also, i cannot append my code to the button that is supposed to unprotect all worksheets via a password...!

sorry for being a hassle; i'm very new to all this...!

hope i'm making myself clear!



best,

Chester
 
Upvote 0
Code:
Sub Unprotect()
rspn = InputBox ("Enter Password")
If rspn ="password" Then
ActiveSheet.Unprotect rspn
Else: MsgBox "Wrong Password" 
End If
End Sub
lenze
 
Upvote 0

Forum statistics

Threads
1,213,544
Messages
6,114,239
Members
448,555
Latest member
RobertJones1986

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