Solved

Unlocking a Sheet for Update, then Relocking

Posted on 2010-09-24
9
275 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now