PDA

View Full Version : Solved: Unprotect with input box



sujittalukde
08-06-2007, 05:49 AM
I am using the following two codes for protecting and unprotecting sheets. The protecting code is working well but for unprotecting the sheet, I am facing problem:
The problem is that even if the user clicks cancel, the sheet gets unprotected. I want that unprotection shall not happen on clicking cancel.


Sub save()
ActiveSheet.Protect Password:="qwe"
End Sub



Sub modify()
InputBox "YOur code", "Modify"
ActiveSheet.Unprotect Password:="qwe"
End Sub

Bob Phillips
08-06-2007, 06:07 AM
Sub modify()
Dim ans
ans = InputBox("YOur code", "Modify")
If ans <> "" Then ActiveSheet.Unprotect Password:="qwe"
End Sub

sujittalukde
08-06-2007, 06:15 AM
Thanks xld, its fully working

sujittalukde
08-07-2007, 04:12 AM
One error found, if the password doesnot match with the password set to protect the sheet, it is unprotecting the sheet. ie Putting any value to the input box unprotecting the sheet. It is not unprotecting if the inoput box is kept blank.

How this can be fixed?

Bob Phillips
08-07-2007, 04:17 AM
[vba]
Sub modify()
Const myPwd as String = "qwe"
Dim ans
ans = InputBox("YOur code", "Modify")
If ans <> "" Then
If ans = myPwd Then ActiveSheet.Unprotect Password:=myPwd

End Sub

Bob Phillips
08-07-2007, 04:21 AM
Sub modify()
Const myPwd as String = "qwe"
Dim ans
ans = InputBox("YOur code", "Modify")
If ans <> "" Then
If ans = myPwd Then ActiveSheet.Unprotect Password:=myPwd
End If
End Sub

sujittalukde
08-07-2007, 04:25 AM
Thanks. I rectified the End If before your last final code. Anyway Thanks