PDA

View Full Version : Code will protect but not unprotect



Sir Babydum GBE
08-19-2011, 07:07 AM
Hi

I've used code to protect a sheet with a password. It works fine

When I use code to unlock with the very same password, I get a "wrong password" error?? :help

When I try to to a manual unlock I also get the password error :banghead:

What the blazes is going on??? :dunno

Thanks

BD

Bob Phillips
08-19-2011, 07:37 AM
Trailing spaces, embedded spaces?

Sir Babydum GBE
08-19-2011, 08:07 AM
Nope, there are no trailing spaces.
Code is:

Sub LockMe()
'
' LockMe Macro
'
Dim myPassword As String
myPassword = "password"
'
ActiveCell.Offset(-1, 0).Range("A1").Copy
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteValidation, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False

With ActiveSheet
.Protect Password = myPassord
.EnableSelection = xlUnlockedCells
End With
End Sub
Sub UnlockMe()
'
' UnlockMe Macro
'
Dim myPassword As String
myPassword = "password"
'
ActiveSheet.Unprotect Password = myPassword
With Selection.Validation
.Delete
.Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _
:=xlBetween
.IgnoreBlank = True
.InCellDropdown = True
.IMEMode = xlIMEModeNoControl
.ShowInput = True
.ShowError = True
End With
Selection.FormulaR1C1 = "=NOW()"
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveCell.Offset(-1, 0).Range("A1").Select
Application.CutCopyMode = False
Selection.Copy
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
End Sub

I just don't get it.

Bob Phillips
08-19-2011, 08:10 AM
They both should be


Password:= myPassord

not


Password = myPassord

and one time you use myPassord the other myPassword. Aren't you using Option Explicit yet?

Sir Babydum GBE
08-19-2011, 08:16 AM
They both should be


Password:= myPassord

not


Password = myPassord

and one time you use myPassord the other myPassword. Aren't you using Option Explicit yet?

Thanks Bob, I feel so silly and childish.