Solved

Unlocking a Sheet for Update, then Relocking

Posted on 2010-09-24
9
284 Views
Last Modified: 2012-05-10
I have a subroutine that refreshes a spreadsheet (Mod), by taking an exact image of the original, yet another sheet (Master) and replacing the modified spreadsheet (Mod). Pretty simple. Now I go to lock certain cells in the master. When I replace the Mod sheet with the subroutine, I now have the protected cells (which I want) in the Mod. sheet. When I do it a second time, it won't allow for it because I have protected certain cells that are now in Mod.  Past EE answers to a similar question yielded the following:

Sub sShow_level()
   ActiveSheet.Unprotect Password:="111"
   ActiveSheet.Outline.ShowLevels RowLevels:=Range("A1").value
   ActiveSheet.Protect Password:="111"
   ActiveSheet.EnableSelection = xlUnlockedCells
End Sub

This routine was to allow for the use of Outlining on a protected sheet.  I need a similar capability to unlock and relock cells when using my subroutine that updates the sheet.  I think this may be a single line change (ActiveSheet.Outline.ShowLevels RowLevels:=Range("A").value) to reflect running my subroutine (Restore_range).

I hope this is clear and easy!

Bright01
0
Comment
Question by:Bright01
  • 5
  • 4
9 Comments
 
LVL 12

Expert Comment

by:tilsant
ID: 33753011
Hi Bright01

You can have the code as

Sub sShow_level()
   ActiveSheet.Unprotect Password:="111"
   Call Restore_Range
   ActiveSheet.Protect Password:="111"
   ActiveSheet.EnableSelection = xlUnlockedCells
End Sub
0
 
LVL 12

Expert Comment

by:tilsant
ID: 33753034
Or other option can be to have 3 Subs, first one to unprotect, second one your sub Restore_Range and the third one to protect.

Then you can call all the three codes one-by-one.

Sub New
Call UnProtect_Sheet
Call Restore_Range
Call Protect_Sheet
End Sub


Sub UnProtect_Sheet
ActiveSheet.Unprotect Password:="111"
End Sub


Sub Protect_Sheet
ActiveSheet.Protect Password:="111"
ActiveSheet.EnableSelection = xlUnlockedCells
End Sub


0
 

Author Comment

by:Bright01
ID: 33753304
Should I put it in a regular spreadsheet, Thisspreadsheet or a Module?
B.
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 12

Expert Comment

by:tilsant
ID: 33753317
you can put all of them in a standard module and then instead of activesheet, you can use Sheet(1), etc....
0
 

Author Comment

by:Bright01
ID: 33753553
I need to rephrase the question.  Since I have two sheets (OAW_Checklist + Master), and I put it in a standard "Module", do I refer to both sheets (see below)? Do I use the Sheet number or the Sheet name?  Then how do I call it from the routine that updates the spreadsheet (further below)?  I'm getting a error with the existing code.......
 The Protect/Unprotect Function

Sub Protect_Unprotect()
Call UnProtect_Sheet
Call Restore_Range
Call Protect_Sheet
End Sub

Sub UnProtect_Sheet()
Sheet(Master).Unprotect Password:="jam"
Sheet(OAW_Checklist).Unprotect Password:="jam"
End Sub

Sub Protect_Sheet()
Sheet(Master).Protect Password:="jam"
Sheet(OAW_Checklist).Protect Password:="jam"
ActiveSheet.EnableSelection = xlUnlockedCells
End Sub
------------------------------------------------------------------
The Code I am using to Restore the Backup
'For Restoring Backup Put in regular module
Sub BackupRange()
Dim rgCopy As Range, rgDest As Range
Dim nmCopy As Name, nmDest As Name
On Error Resume Next
Set nmCopy = ThisWorkbook.Names("Original")
Set nmDest = ThisWorkbook.Names("Copy")
On Error GoTo 0
If nmCopy Is Nothing Then
    Set rgCopy = Worksheets("OAW Checklist").Range("B3:DR706")
    Set nmCopy = ThisWorkbook.Names.Add("Copy", "=" & "'" & Replace(rgCopy.Worksheet.Name, "'", "''") & "'!" & rgCopy.Address)
End If
If nmDest Is Nothing Then
    Set rgDest = Worksheets("Master").Range("B3:DR706")
    Set nmDest = ThisWorkbook.Names.Add("Original", "=" & "'" & Replace(rgDest.Worksheet.Name, "'", "''") & "'!" & rgDest.Address)
End If
Set rgCopy = nmCopy.RefersToRange
Set rgDest = nmDest.RefersToRange
rgCopy.Copy rgDest
End Sub
 
 
0
 

Author Comment

by:Bright01
ID: 33754087
Let me do better..... I've attached the actual workbook. It is unprotected. If you protect cells (C:10-C:16) on the OAW_Checklist sheet and then try to restore the code, you will see the debug error I'm dealing with and what I'm trying to do by unlocking and relocking the sheet.
Hope that helps.
 B.

Protect-Unprotect-Sample-Code.xlsm
0
 
LVL 12

Accepted Solution

by:
tilsant earned 500 total points
ID: 33754827
Hi B,

I have added 2 lines in your RestoreRange Sub to call UnProtect and Protect Macro and moved your codes in Module 5 from Module 6.

You can change the Protection of whichever cells you wish to Protect and then protect the sheet once with the password "jam" and then run the "Confirm Macro" to check.


Tils.
'Put in regular module
Sub RestoreRange()

'***** To unprotect the sheets ******
Call UnProtect_Sheet

Dim rgCopy As Range, rgDest As Range
Dim selectedRange As String
Set rgCopy = ThisWorkbook.Names("Copy").RefersToRange
Set rgDest = ThisWorkbook.Names("Original").RefersToRange
Application.EnableEvents = False
rgCopy.Formula = rgDest.Formula
selectedRange = Selection.Address
rgDest.Copy
rgCopy.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
ActiveSheet.Range(selectedRange).Select
Application.EnableEvents = True

'***** To protect the sheets ******
Call Protect_Sheet

End Sub

'***** To unprotect the sheets ******
Sub UnProtect_Sheet()
Sheet4.Unprotect Password:="jam"
Sheet2.Unprotect Password:="jam"
End Sub

'***** To protect the sheets ******
Sub Protect_Sheet()
Sheet4.Protect Password:="jam"
Sheet2.Protect Password:="jam"
ActiveSheet.EnableSelection = xlUnlockedCells
End Sub

Open in new window

Protect-Unprotect-Sample-Code.xlsm
0
 

Author Closing Comment

by:Bright01
ID: 33755767
Tils,

Fantastic!  Thank you very very much.  It works very well.  I'm going to be asking a related question.  I have other activities that I'm going to have to deal with and there is a command that allows for routines to execute between themselves while keeping the user interface protected..........  Again...much thanks!
0
 
LVL 12

Expert Comment

by:tilsant
ID: 33759272
Thanks B!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question