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'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