T
Tanya
Hi,
I have the following code, thanks to this forum and some very helpful people
and I have just noticed that when I run the by way of the first code below
and select cancel, I am taken to the admin sheet which is what I would like
to avoid. What code to I need to add so that if cancel is selected I am not
taken to Admin Sheet?
Private Sub CommandButton1_Click()
'Unprotect workbook
UnProtect_Workbook
'Show Admin Sheet
Sheets("Admin").Visible = True
Sheets("Admin").Select
End Sub
Sub UnProtect_Workbook()
'Unprotect workbook
Dim ws As Worksheet
Dim myPwd As String
Application.ScreenUpdating = False
Set ws = Worksheets(1)
On Error Resume Next
Do
myPwd = GetPassword
ws.Unprotect Password:=myPwd
If myPwd <> "" Then
If ws.ProtectContents Then
MsgBox "Invalid password, try again", vbOKOnly +
vbInformation, "Password input"
End If
End If
Loop Until Not ws.ProtectContents Or myPwd = ""
On Error GoTo 0
If myPwd <> "" Then
For Each ws In ActiveWorkbook.Worksheets
If ws.ProtectContents = True Then
ws.Unprotect Password:=myPwd
End If
Next ws
ActiveWorkbook.Unprotect Password:=myPwd
Application.ScreenUpdating = True
End If
End Sub
Private Function GetPassword() As Variant
GetPassword = InputBox(Prompt:="Please enter correct Password")
End Function
Thanks in advance
Cheers
Tanya
I have the following code, thanks to this forum and some very helpful people
and I have just noticed that when I run the by way of the first code below
and select cancel, I am taken to the admin sheet which is what I would like
to avoid. What code to I need to add so that if cancel is selected I am not
taken to Admin Sheet?
Private Sub CommandButton1_Click()
'Unprotect workbook
UnProtect_Workbook
'Show Admin Sheet
Sheets("Admin").Visible = True
Sheets("Admin").Select
End Sub
Sub UnProtect_Workbook()
'Unprotect workbook
Dim ws As Worksheet
Dim myPwd As String
Application.ScreenUpdating = False
Set ws = Worksheets(1)
On Error Resume Next
Do
myPwd = GetPassword
ws.Unprotect Password:=myPwd
If myPwd <> "" Then
If ws.ProtectContents Then
MsgBox "Invalid password, try again", vbOKOnly +
vbInformation, "Password input"
End If
End If
Loop Until Not ws.ProtectContents Or myPwd = ""
On Error GoTo 0
If myPwd <> "" Then
For Each ws In ActiveWorkbook.Worksheets
If ws.ProtectContents = True Then
ws.Unprotect Password:=myPwd
End If
Next ws
ActiveWorkbook.Unprotect Password:=myPwd
Application.ScreenUpdating = True
End If
End Sub
Private Function GetPassword() As Variant
GetPassword = InputBox(Prompt:="Please enter correct Password")
End Function
Thanks in advance
Cheers
Tanya