Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
GS GS is offline
external usenet poster
 
Posts: 364
Default Protecting Worksheets Problem

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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
protecting worksheets Anthony Excel Worksheet Functions 1 July 27th 08 10:59 PM
Protecting Worksheets Pistols14 Excel Discussion (Misc queries) 7 November 27th 07 10:14 AM
Protecting Worksheets Kaitlin.uk Excel Discussion (Misc queries) 1 January 18th 07 02:21 PM
Protecting worksheets keiron James Excel Worksheet Functions 1 September 13th 06 04:23 PM
Protecting Worksheets Kathy081403 Excel Worksheet Functions 2 January 12th 06 12:56 AM


All times are GMT +1. The time now is 08:35 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"