Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi Andy,
The problem may be that a different password was used to protect the workbook. The thing is, your code is very confusing at first. There's way too many variable names being used for the same values. This may be a contributing factor to password problems. Another thing is it's not necessary to remove workbook protection to do worksheet related actions. Just unprotect the sheet, do your thing, then re-apply protection. How I "might" handle what you're trying to do is: 1. I would declare the common variables with global scope so their values persist while the workbook is open. 2. I use separate procedures for sheet protect/unprotect so I can access them randomly, on demand. Ordinarily I would hard code the password in the procedures, but since you're sharing it globally AND storing it on the worksheet, it can be stored in a global variable and shared while the project is open. I wrote some sample code that you may find helpful. I changed the names of your procedures to better reflect their nature. If you use these names, you'll need to re-assign the shortcuts. I hope this is helpful! Regards, GS Here's the code: (It goes in a standard module) Option Explicit 'Declare global variables Dim gszPassword1 As String Dim gszPassword2 As String Dim grngPass1 As Range Sub SetProtection() 'alias: ProtectSheet() ' Locks Sheet and Workbook with inputed password ' KeyboardShortcut: Ctrl+L 'Get password gszPassword1 = InputBox("Type a password") gszPassword2 = InputBox("Re-type Password") 'Reference where the password is stored Set grngPass1 = Worksheets("BID RECAP SUMMARY").Range("A160") If gszPassword1 < gszPassword2 Then 'Verify password MsgBox ("Passwords didnt match, please try again.") Else 'Store the password grngPass1.Value = gszPassword1 'Apply protection WksProtect ActiveWorkbook.Protect Password:=gszPassword1, Structu=True 'Display notification MsgBox ("Password is set as " & gszPassword1) End If End Sub Sub RemoveProtection() 'Alias: UnProtectSheet() ' UnLocks Sheet and Workbook ' KeyboardShortcut: Ctrl+Shift+L 'Reference where the password is stored Set grngPass1 = Worksheets("BID RECAP SUMMARY").Range("A160") 'Get the password gszPassword1 = InputBox("Type password to remove protection." _ & vbCrLf & " Password is " & grngPass1.Value) 'Verify password If LCase$(gszPassword1) < LCase$(grngPass1.Value) Then Exit Sub 'Remove protection WksUnprotect ActiveWorkbook.Unprotect gszPassword1 End Sub Sub WksProtect() With ActiveSheet .Protect Password:=gszPassword1, _ DrawingObjects:=True, _ Contents:=True, _ Scenarios:=True, _ Userinterfaceonly:=True .EnableSelection = xlUnlockedCells End With End Sub Sub WksUnprotect() ActiveSheet.Unprotect gszPassword1 End Sub |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
protecting worksheets | Excel Worksheet Functions | |||
Protecting Worksheets | Excel Discussion (Misc queries) | |||
Protecting Worksheets | Excel Discussion (Misc queries) | |||
Protecting worksheets | Excel Worksheet Functions | |||
Protecting Worksheets | Excel Worksheet Functions |