copy sheet level protection

ajm

Well-known Member
Joined
Feb 5, 2003
Messages
2,005
Office Version
  1. 365
Platform
  1. Windows
Folks, i have a management report supported by ten or so individual kpi reports. the kpi reports are all exactly the same as each other except each relates to a different site. all the tab names are the same and all the tabs contain the same structure. each KPI workbook is handled by another person. I have been asked to lock down the KPI reports so that only specific cells can be written to. this is, as you will all know, to stop the user changing anything they are not supposed to. I have gone through the first workbook and locked all the cells that need locking. I am ready to do the sheet level protection now and then on to the next one. its just so tedious. does anyone know how to copy the locked cell format from each tab within this "source" file to each of the other tabs respectively within the other workbooks
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Folks, i have a management report supported by ten or so individual kpi reports. the kpi reports are all exactly the same as each other except each relates to a different site. all the tab names are the same and all the tabs contain the same structure. each KPI workbook is handled by another person. I have been asked to lock down the KPI reports so that only specific cells can be written to. this is, as you will all know, to stop the user changing anything they are not supposed to. I have gone through the first workbook and locked all the cells that need locking. I am ready to do the sheet level protection now and then on to the next one. its just so tedious. does anyone know how to copy the locked cell format from each tab within this "source" file to each of the other tabs respectively within the other workbooks

Sounds like VBA would help here. Absent your workbook, my solution will require editing. Just to be clear though, you're locking certain cells on a sheet and then locking the sheet, and all of this will match identically to the other workbooks?
 
Upvote 0
This is draft #1 of my solution. It will speed up the process, but I still need to make some changes so that it applies to subsequent workbooks. Please try this out on a workbook. Basically, once I've finished it then it will remember all the sheet names and range addresses, so all you'll need to do is select a workbook to apply further protection.

VBA Code:
Sub Protectimus()

    Dim ws() As Worksheet
    Dim rg() As Range
    Dim pw As String
   
    pw = InputBox("Please type password for locking sheets.  If no PW, just hit Enter", "Password Entry")
   
    ReDim ws(1 To Sheets.Count)

    ReDim rg(1 To Sheets.Count)
   
        For i = 1 To Sheets.Count
       
            Set ws(i) = Sheets(Sheets(i).Name)
            ws(i).Activate
            ws(i).Cells.Locked = False
            Set rg(i) = Application.InputBox(prompt:="Select all ranges on " & ws(i).Name & " that require locking, then hit OK.  Hold CTRL to make multiple selections!", _
                                                    Title:="Selection", Type:=8)
            rg(i).Locked = True
            ws(i).Protect Password:=pw, DrawingObjects:=True, Contents:=True, Scenarios:=True
           
        Next
                               
End Sub
 
Upvote 0
What I suggest you do for greater flexibility, is give all unlocked cells a certain colour.

This not only makes it easier for users to see what can be changed, but then the code can go through and lock all the cells except those with the special colour (so you don't need to select them manually).
 
Upvote 0
This code will lock all the sheets, but will leave all the cells coloured yellow (colorindex=6) unlocked

VBA Code:
Sub LockSheets()
  Dim r, sht
  For Each sht In Sheets
    Application.StatusBar = "Protecting " & sht.Name
    sht.Unprotect
    sht.UsedRange.Cells.Locked = True
    For Each r In sht.UsedRange
      If r.Interior.ColorIndex = 6 Then r.Locked = False
    Next r
    sht.Protect
  Next sht
  Application.StatusBar = False
End Sub

You will want a different colour. To get its number, colour a cell, then run this code (with the coloured cell selected)
VBA Code:
Sub GetColor()
  MsgBox Selection.Interior.ColorIndex
End Sub
 
Upvote 0
Sounds like VBA would help here. Absent your workbook, my solution will require editing. Just to be clear though, you're locking certain cells on a sheet and then locking the sheet, and all of this will match identically to the other workbooks?
yes. each sheet in the one workbook i have completed is the same as each sheet in each of the other workbooks. i started with one file and then copied it and replaced the data for each new site. i forgot about the locking of cells until after i had finished all the reports.
 
Upvote 0
This code will lock all the sheets, but will leave all the cells coloured yellow (colorindex=6) unlocked

VBA Code:
Sub LockSheets()
  Dim r, sht
  For Each sht In Sheets
    Application.StatusBar = "Protecting " & sht.Name
    sht.Unprotect
    sht.UsedRange.Cells.Locked = True
    For Each r In sht.UsedRange
      If r.Interior.ColorIndex = 6 Then r.Locked = False
    Next r
    sht.Protect
  Next sht
  Application.StatusBar = False
End Sub

You will want a different colour. To get its number, colour a cell, then run this code (with the coloured cell selected)
VBA Code:
Sub GetColor()
  MsgBox Selection.Interior.ColorIndex
End Sub
i use colour to identify the cells that my users aren't allowed to touch. this could work nicely. i'll just add to the sht.Protect with a password and other arguments and we're good to go.
 
Last edited:
Upvote 0
@gravanoc,@Dermot

thanks for your help. i started building a version of each of the two methods. then, thought it'd be really cool to build a combined version using the ideas from both of your contributions. then realised it was 11.30, the movie had ended, and i had just typed a thousand d's across the page, maybe more. this morning, i was reformatting one of the models with changes i had made to my original (the one with the locked cells already done), and came to the realisation that using the format painter copies the lock settings also. so, now, i am just using it to get my job finished asap. its a sunday and i have been looking at little rectangles all week. i need a break. maybe i'll play some minecraft......
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top