Solved

Unlocking a Sheet for Update, then Relocking

Posted on 2010-09-24
9
286 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

726 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