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
 
I suppose that works!! I, however, would write it this way
Rich (BB code):
Sub Unprotect_workbook()
Dim ws as WorkSheet
rspn = InputBox("Please enter password", "Password required")
If rspn <> "AS CSM" Then
    MsgBox "Incorrect password: workbook could not be unprotected"
Else:
For Each ws in Sheets
    ws.UnProtect rspn
Next ws
MsgBox "Workbook is unprotected"
End if
End Sub
lenze
 
Upvote 0

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
okay, i'll do as you stated! ;)

is there a way i can close the message box upon clicking the upper right hand cross (i.e., close dialogue box) or upon clicking cancel...? unload me or so?



best,

Chester
 
Upvote 0
Have you not tried that?? You really should try the solutions offered before asking irrelevant questions. If you had done so, you would see that the MsgBox will unload when "OK" is clicked!!
lenze
 
Upvote 0
sorry Lenze,

i don't know what solutions you're getting at... as said, i'm completely new to VBA and am trying to find my way around, so pardon my ignorance if this may seem so... i'm not trying to annoy or other, but am simply trying to get on with building my model.

so what is the best way for me to close the message box if i click the close dialogue box or cancel? would that be the unload function then?



cheers and my excuses!

Chester
 
Upvote 0

Forum statistics

Threads
1,215,884
Messages
6,127,567
Members
449,385
Latest member
KMGLarson

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