T
Tanya
Hi Dave
The code I have now closes the form once commandbutton1 is selected
regardless of whether the password was correct or not. How do I change the
code to prompt user to enter the password again?
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Dim eCtr As Long
Dim wbUnprotected As Boolean
Dim myStr As String
Dim myPwd As String
Application.ScreenUpdating = False
myPwd = Me.txtUnprotect.Value
eCtr = 0
For Each ws In ActiveWorkbook.Worksheets
If ws.ProtectContents = True _
Or ws.ProtectDrawingObjects = True _
Or ws.ProtectScenarios = True Then
On Error Resume Next
ws.Unprotect Password:=myPwd
If Err.Number <> 0 Then
eCtr = eCtr + 1
Err.Clear
End If
On Error GoTo 0
End If
Next ws
wbUnprotected = True
If ActiveWorkbook.ProtectStructure = True _
Or ActiveWorkbook.ProtectWindows = True Then
On Error Resume Next
ActiveWorkbook.Unprotect Password:=myPwd
If Err.Number <> 0 Then
wbUnprotected = False
Err.Clear
End If
On Error GoTo 0
End If
myStr = ""
If wbUnprotected = False Then
myStr = "Workbook Not Unprotected"
End If
If eCtr > 0 Then
If myStr <> "" Then
myStr = myStr & vbLf
End If
myStr = myStr & eCtr & " worksheets not unprotected!"
End If
If myStr = "" Then
myStr = "Workbook and all worksheets unprotected"
End If
Me.Label1.Caption = myStr
Application.ScreenUpdating = True
Me.Label1.Caption = ""
Unload Me
If wbUnprotected = True Then
Worksheets("Admin").Select
Range("A1").Select
ActiveWindow.DisplayWorkbookTabs = True
End If
End Sub
Kind Regards
Tanya
The code I have now closes the form once commandbutton1 is selected
regardless of whether the password was correct or not. How do I change the
code to prompt user to enter the password again?
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Dim eCtr As Long
Dim wbUnprotected As Boolean
Dim myStr As String
Dim myPwd As String
Application.ScreenUpdating = False
myPwd = Me.txtUnprotect.Value
eCtr = 0
For Each ws In ActiveWorkbook.Worksheets
If ws.ProtectContents = True _
Or ws.ProtectDrawingObjects = True _
Or ws.ProtectScenarios = True Then
On Error Resume Next
ws.Unprotect Password:=myPwd
If Err.Number <> 0 Then
eCtr = eCtr + 1
Err.Clear
End If
On Error GoTo 0
End If
Next ws
wbUnprotected = True
If ActiveWorkbook.ProtectStructure = True _
Or ActiveWorkbook.ProtectWindows = True Then
On Error Resume Next
ActiveWorkbook.Unprotect Password:=myPwd
If Err.Number <> 0 Then
wbUnprotected = False
Err.Clear
End If
On Error GoTo 0
End If
myStr = ""
If wbUnprotected = False Then
myStr = "Workbook Not Unprotected"
End If
If eCtr > 0 Then
If myStr <> "" Then
myStr = myStr & vbLf
End If
myStr = myStr & eCtr & " worksheets not unprotected!"
End If
If myStr = "" Then
myStr = "Workbook and all worksheets unprotected"
End If
Me.Label1.Caption = myStr
Application.ScreenUpdating = True
Me.Label1.Caption = ""
Unload Me
If wbUnprotected = True Then
Worksheets("Admin").Select
Range("A1").Select
ActiveWindow.DisplayWorkbookTabs = True
End If
End Sub
Kind Regards
Tanya