Protect All Sheets Then Save

  • I have a workbook with several Sheets. I wish to automatically protect all sheets with specific user permissions, i.e. Select Unlocked Cells, Insert Rows, Use Auto Filter and Edit Objects, as I would have available when using the manual approach. Also, I wish to invoke a Password in the process.


    Additionally, I need this code to operate when the file is Saved or Closed... transparent to the user. Any ideas out there?

  • Re: Protect All Sheets Then Close Workbook


    Use the macro recorder to produce the code you want for protecting a sheet then insert it in this!

    Code
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    For Each Sheet In Sheets
    'YOUR PROTECT CODE HERE
    Next Sheet
    ThisWorkbook.Save
    
    
    End Sub

    this code goes in the ThisWorkbook module.

  • Re: Protect All Sheets Then Close Workbook


    Quote from Simon Lloyd

    Use the macro recorder to produce the code you want for protecting a sheet then insert it in this!

    Code
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    For Each Sheet In Sheets
    'YOUR PROTECT CODE HERE
    Next Sheet
    ThisWorkbook.Save
    
    
    End Sub

    this code goes in the ThisWorkbook module.



    Quote from velnoskt

    Thanks, I tried this code and got a Compile Error: For Without Next What did I do incorrectly?


  • Re: Protect All Sheets Then Save


    Try this:


    Code
    Dim ws As Worksheet
        For Each ws In ActiveWorkbook.Sheets(Array("Sheet1", "Sheet2", "Sheet3"))
        With ws
            .Protect Password:="MyPassword", DrawingObjects:=True, _
            contents:=True, Scenarios:=True, userinterfaceonly:=True
            .EnableAutoFilter = True
        End With
        Next ws


    I tend to use it in the workbook open event, rather than close, but it will work either way.

  • Re: Protect All Sheets Then Save


    Welcome to osgrid, velnoskt. Tell your friends about us.


    Quote

    Thanks, I tried this code and got a Compile Error: For Without Next What did I do incorrectly?


    You left the Next statment out. Also you should Dim wsSheet as Worksheet (and always use Option Explicit)


    [FONT="Arial Black"][COLOR="blue"][SIZE="4"]Bill[/SIZE][/COLOR][/FONT]
    Tip: To avoid chasing code always use Option Explicit.

  • Re: Protect All Sheets Then Save


    Thanks for picking that up Bill, if you were to use AAE's solution you would have to specify each and every sheet in the array, the way i supplied will run through each sheet regardless, and as Bill said if you use a variable for anything the Declare it otherwise Excel will just treat it as variant, here's the code tidied, you don't need to name each sheet like sheet1.protect.... sheet2.protect because you are using the same settings for each sheet!

  • Re: Protect All Sheets Then Save


    [QUOTE]Simon & Bill, Thank you both for your valuable time and help... Simon's simplified CODE worked great and I added my password...THANKS!!!
    QUOTE]


    [hr]*[/hr] Auto Merged Post Until 24 Hrs Passes;[dl]*[/dl]Simon & Bill, I had forgotten to see if just SAVING the document invoked the protection... it does not... Thoughts?
    Thanks
    Tom

  • Re: Protect All Sheets Then Save


    Thanks AAE for you contribution



    velnoskt, no need to put quote tags around your own posts. Your not quoting yourself

    [FONT="Arial Black"][COLOR="blue"][SIZE="4"]Bill[/SIZE][/COLOR][/FONT]
    Tip: To avoid chasing code always use Option Explicit.

  • Re: Protect All Sheets Then Save


    Velknost, you would have to have the code in another module like this:
    these go in the ThisWorkbook module:

    Code
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Call MyProtect
    End Sub
    
    
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Call MyProtect
    End Sub


    This one goes in a standard module:

    Code
    Sub MyProtect()
    Dim wsSheet As Worksheet
        For Each wsSheet In Sheets
            wsSheet.Protect Password:="p", DrawingObjects:=False, Contents:=True, Scenarios:= _
            True, AllowInsertingRows:=True, AllowFiltering:=True
            wsSheet.EnableSelection = xlUnlockedCells
        Next wsSheet
         ThisWorkbook.Save
    End Sub
  • Re: Protect All Sheets Then Save


    Thank You Simon... Works Great... I am assuming too that I can add other duties in this script before closing, such as unhiding all rows, and returning to A1 etc.. as well.. Thanks .-- Tell me... is it within the rules to contact one another via email for continued interaction... don't want to break the rules so just tell me


    Tom

  • Re: Protect All Sheets Then Save


    Say Siman,


    I don't understand your post?


    Code
    Dim wsSheet As Worksheet 
        For Each wsSheet In Sheets 
            wsSheet.Protect Password:="p", DrawingObjects:=False, Contents:=True, Scenarios:= _ 
            True, AllowInsertingRows:=True, AllowFiltering:=True 
            wsSheet.EnableSelection = xlUnlockedCells 
        Next wsSheet 
        ThisWorkbook.Save


    Works ok as standalone code in Thisworkbook BeforeClose or BeforeSave[hr]*[/hr] Auto Merged Post Until 24 Hrs Passes;[dl]*[/dl]velnoskt, Appreciate your inquire as to not break any rules.
    All questions and answers should be posted to the forum. The goal of the forum is to teach and learn for all to see. How can the rest of us learn if you use Private Message or email?


    Thanks for using ozgrid

    [FONT="Arial Black"][COLOR="blue"][SIZE="4"]Bill[/SIZE][/COLOR][/FONT]
    Tip: To avoid chasing code always use Option Explicit.

  • Re: Protect All Sheets Then Save


    Bill, i provided that alternative because of this at the end of Tom's post:

    Quote from Tom

    Simon & Bill, I had forgotten to see if just SAVING the document invoked the protection... it does not... Thoughts?
    Thanks
    Tom

    I just thought it would better demonstrate to Tom the events that were happening rather than just copying the code to the Before_Save[hr]*[/hr] Auto Merged Post Until 24 Hrs Passes;[dl]*[/dl]Also Bill Tom posted this:

    Quote from Tom

    I am assuming too that I can add other duties in this script before closing, such as unhiding all rows, and returning to A1 etc

    so it saves on confusion for him if he is trying to integrate other code with what he already had in the before close, now he has a clean sheet to work with!

  • Re: Protect All Sheets Then Save


    Thanks Simon

    [FONT="Arial Black"][COLOR="blue"][SIZE="4"]Bill[/SIZE][/COLOR][/FONT]
    Tip: To avoid chasing code always use Option Explicit.

  • Re: Protect All Sheets Then Save


    Thanks Simon.... The reason I was asking is to find someone to help develop some CODE for our Non-Profit ORG when those times come around.. not so much to ask a quaestion...
    Anyway you did me GOOD and I learned a great deal... Thanks Again.
    Tom

  • Re: Protect All Sheets Then Save


    Well, i am glad that Bill, I and Ozgrid could help you out, we just push in the right direction we cant make you learn but if you get something from it all the better....all the best.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!