Results 1 to 8 of 8

Thread: Protect All Worksheets Before Closing The Workbook

  1. #1
    Member
    Join Date
    Dec 2012
    Posts
    78
    Rep Power
    13

    Protect All Worksheets Before Closing The Workbook

    Hi

    I require a code when closing workbook.When closing workbook that all sheets are in protect mode.
    Also I have a commandbutton1 in sheet Weekly Graph and a commandbutton1 in Score Sheet. When the workbook is closed and all sheets are protected the 2 commandbuttons say Unprotect Sheet.When I then open the workbook the commandbutton1 in Weekly Graph says Unprotect Sheet and the commandbutton1 in Score sheet says Unprotect Sheet.

    The 2 commandbuttons have the captions Protect Sheet and Unprotect Sheet

    Thanks

  2. #2
    Moderator
    Join Date
    Jul 2012
    Posts
    156
    Rep Power
    13
    Do you try anything yourself at all ??
    You've just been given a code to protect all sheets on opening the workbook. Use the same code but this time in the Workbook_BeforeClose-event.

  3. #3
    Member
    Join Date
    Dec 2012
    Posts
    78
    Rep Power
    13
    I don`t remember asking for your help specificly...there are far more clever people on here then you......it is pretty obvious i do not know anything about code....

  4. #4
    Moderator
    Join Date
    Jul 2012
    Posts
    156
    Rep Power
    13
    Then I advise you to get a good book about VBA so that you at least understand the basics of what is written here.

    there are far more clever people on here then you
    How can you evaluate that since you don't know anything about code ????

  5. #5
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    rich, whether you know code or not, disregard to another member will not be entertained. baker only implied that if you do a trial and error, you will learn something yourself, and not have to rely on the forum for every aspect of coding, and only ask things that are beyond your comprehension. anyway, I for one do not mind anyone here asking questions that are seemingly straightforward or plain simple, however, we at the forum do have issues with targeted comments that are a little too personal in nature. If such an offense is repeated, your posting privileges in this forum may be revoked, temporarily or permanently, depending on the offense.
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  6. #6
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    And here's what you're looking for

    Code:
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    
        Dim sht As Object
        Dim blnSaved As Boolean
        
        blnSaved = ThisWorkbook.Saved
        For Each sht In ThisWorkbook.Sheets
            sht.Unprotect Password:="Password"
            sht.Protect Password:="Password", UserInterfaceOnly:=True
        Next sht
        Worksheets("Weekly Graph").CommandButton1.Caption = "Unprotect Sheet"
        Worksheets("Score Sheet").CommandButton1.Caption = "Unprotect Sheet"
        If blnSaved Then
            ThisWorkbook.Save
        End If
        
    End Sub
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  7. #7
    Member
    Join Date
    Dec 2012
    Posts
    78
    Rep Power
    13
    First thanks for the code.I have never personally attacked anyone here before and have thanked anyone for there help...I have no issues with users saying find a good VBA book to learn the basics which is a fair call but I find this comment personal as well which is why I responded - Do you try anything yourself??? ....People read things in different ways

    I did have a code which did work for 1 sheet but I could not get it to work for multiple sheets.

    I accept your decision to ban my membership if required


    Thanks

    Rich
    Last edited by rich_cirillo; 07-14-2013 at 01:53 PM.

  8. #8
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    baker could probably have rephrased that line as a more indirect one, still implying what was intended. having said that, rich, you didn't have to revert with the comment targeting baker in response to his question of trying things yourself. yes people read things differently, but to me, rich's was a more personal assault than anything.

    anyway, I'll be keeping a watch. any defense to your action in this specific incident will be considered as an offense.

    closing the thread here as this conversation is over. period.
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

Similar Threads

  1. To Fin out Purchases against Closing stock
    By Prabhu in forum Excel Help
    Replies: 1
    Last Post: 05-15-2013, 09:48 AM
  2. How To Protect Hide Formula In Excel
    By paul_pearson in forum Excel Help
    Replies: 1
    Last Post: 04-10-2013, 08:33 AM
  3. Replies: 22
    Last Post: 03-19-2013, 07:57 AM
  4. Replies: 2
    Last Post: 12-04-2012, 02:05 PM
  5. Closing Stock Report With Parameters
    By Prabhu in forum Excel Help
    Replies: 8
    Last Post: 05-08-2012, 02:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •