• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

Unlocking a Sheet for Update, then Relocking

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
Bright01
Asked:
Bright01
  • 5
  • 4
1 Solution
 
tilsantCommented:
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
 
tilsantCommented:
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
 
Bright01Author Commented:
Should I put it in a regular spreadsheet, Thisspreadsheet or a Module?
B.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
tilsantCommented:
you can put all of them in a standard module and then instead of activesheet, you can use Sheet(1), etc....
0
 
Bright01Author Commented:
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
 
Bright01Author Commented:
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
 
tilsantCommented:
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
 
Bright01Author Commented:
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
 
tilsantCommented:
Thanks B!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now