Solved

Unlocking a Sheet for Update, then Relocking

Posted on 2010-09-24
9
282 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

822 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