Consulting

Results 1 to 12 of 12

Thread: Solved: Code to unprotect only one worksheet

  1. #1
    VBAX Contributor
    Joined
    Mar 2007
    Posts
    140
    Location

    Solved: Code to unprotect only one worksheet

    Hello All,

    I have a workbook with 13 worksheets in it.
    I have a macro to protect all worksheets when the workbook is opened, but I would like one of the worksheet to remain unprotected.

    Workbook_Open code:
    Private Sub Workbook_Open()
      Dim Sh As Worksheet
        Application.ScreenUpdating = False
        Application.WindowState = xlMaximized
        ActiveWindow.WindowState = xlMaximized
      For Each Sh In ThisWorkbook.Worksheets
        Sh.Select
        Sh.Protect userinterfaceonly:=True
        Sh.EnableSelection = xlNoRestrictions
        Sh.EnableAutoFilter = True
        Sh.EnableOutlining = True
      On Error Resume Next
        Sh.ShowAllData
        Err.Clear
        Application.ScreenUpdating = True
    End Sub
    I have been trying to use a Worksheet_Activate macro to unprotect a worksheet called "FINANCIAL YR INSPECT & MONITOR" wihout success:
    Private Sub Worksheet_Activate()
      On Error Resume Next
      ActiveSheet.Unprotect
    End Sub
    Is there a better way ???

    Can someone please give me two explanations along with code suggestions:
    1. Can extra code be added to the Workbook_Open code to exclude a specific worksheet
    2. What am I doing wrong with the Worksheet_Activate macro
    Thanking you in advance.

    Regards,
    Dave T

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    This fixes your immediate question[vba]Private Sub Workbook_Open()
    Dim Sh As Worksheet
    Application.ScreenUpdating = False
    Application.WindowState = xlMaximized
    ActiveWindow.WindowState = xlMaximized
    For Each Sh In ThisWorkbook.Worksheets
    Sh.Select
    If Sh.Name <> "MySheet" Then ' change for sheet name
    Sh.Protect userinterfaceonly:=True
    End If
    Sh.EnableSelection = xlNoRestrictions
    Sh.EnableAutoFilter = True
    Sh.EnableOutlining = True
    On Error Resume Next
    Sh.ShowAllData
    Err.Clear
    Application.ScreenUpdating = True
    End Sub
    [/vba]I wouldn't use On error Resume next as it masks all problems, maybe this is a little tidier[vba]Private Sub Workbook_Open()
    Dim Sh As Worksheet
    Application.ScreenUpdating = False
    Application.WindowState = xlMaximized
    ActiveWindow.WindowState = xlMaximized
    For Each Sh In Sheets
    With Sh
    If .Name <> "MySheet" Then ' change for sheet name
    .Protect userinterfaceonly:=True
    End If
    .EnableSelection = xlNoRestrictions
    .EnableAutoFilter = True
    .EnableOutlining = True
    .ShowAllData
    End With
    Next Sh
    Application.ScreenUpdating = True
    End Sub
    [/vba]You don't need to select a sheet in order to manipulate it.
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    How's this?

    [vba]Private Sub Workbook_Open()
    Dim Sh As Worksheet
    With Application
    .ScreenUpdating = False
    .WindowState = xlMaximized
    End With
    ActiveWindow.WindowState = xlMaximized
    For Each Sh In ThisWorkbook.Worksheets
    With Sh
    If Not UCase(.Name) = "FINANCIAL YR INSPECT & MONITOR" Then
    .Protect userinterfaceonly:=True
    .EnableSelection = xlNoRestrictions
    .EnableAutoFilter = True
    .EnableOutlining = True
    On Error Resume Next
    .ShowAllData
    On Error GoTo 0
    End If
    End With
    Next Sh
    Application.ScreenUpdating = True
    End Sub[/vba]
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  4. #4
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Beat me by a minute there, Simon. Just for reference, if I recall correctly, ShowAllData throws an error if filtering is not active. So you would want to have the On Error in front, but I'd suggest that it be turned off immediately after.

    Dave, so you know, setting the On Error Goto 0 clears any reference to the error as well, so you don't need to use err.clear with it.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  5. #5
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    And hey, Dave, thank you. I learned something from this thread.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  6. #6
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Quote Originally Posted by Ken Puls
    Beat me by a minute there, Simon. Just for reference, if I recall correctly, ShowAllData throws an error if filtering is not active. So you would want to have the On Error in front, but I'd suggest that it be turned off immediately after.
    Good catch, i didn't test it (bad i know ) it was early here and i just typed it up. I'm not a fan of "On Error Resume Next" but in some cases i guess you have to bite the bullet
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  7. #7
    VBAX Contributor
    Joined
    Mar 2007
    Posts
    140
    Location
    Thank you Simon and Ken for your replies, they are very much appreciated.

    Just out of curiosity...
    Could you use code along the lines of both of yours but modified to protect the entire workbook and if desired use Worksheet_Active on a sheet by sheet basis to unprotect a specific sheet.
    I realise this would be an inefficient way to do things i.e. protect all worksheets then unprotect one/some.
    Would there be any problems/conflict protecting all worksheets with the Workbook_Open code then possibly soon after a Worksheet_Activate code is run that unprotects a worksheet ??
    If there were no problems what would the Worksheet_Activate code look like if run in conjunction with the Workbook_open ??

    Once again, thanks for your replies.

    Regards,
    Dave T

  8. #8
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    If you unprotect on Worksheet_Activate then there is no point whatsoever in protecting the worksheet.

    Protecting the workbook will protect the structure too and you will NOT be able to make any changes without unprotecting. I don't know what you envisage doing, can you explain better what you want to acheive and why?
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  9. #9
    VBAX Contributor
    Joined
    Mar 2007
    Posts
    140
    Location
    Hello Simon,

    I agree the code in the Workbook_Open as suggested by you and Ken is the way I will go.
    The workbook I have used the code in will have all the sheets protected.
    All I was curious about was that rather than adding extra lines to the Workbook_Open coding for each sheet/s, could Worksheet_Activate be included on a sheet by sheet basis ??
    If this did not cause any conflict with the Workbook_Open maybe it could be temporarily added to various worksheets while they are being worked on and then removed later.
    Also I suppose I thought it might be easier to add a block of unique, self contained code as required, at bit like where you call another macro from within other code. That way it might be easier for a VBA novice like me to remove it when it is no longer required than trying to work out which parts of the Workbook_Open code can be removed/commented out without affecting the original code.

    Not that I am planning of using the Worksheet_Activate code, I was curious if trying to unprotect as worksheet using Worksheet_Activate would cause any conflict if a Workbook_Open macro is used that protects all of the sheets in the workbook upon opening.

    Regards,
    Dave T

  10. #10
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    It wouldn't cause a conflict (although as i said unprotect on activate means the whole worksheet is ediatble everytime it's brought in to view) as the Workbook_Open code runs first and then is no longer referenced, i haven't tried it so not sure that the worksheet_activate runs on the initial visible sheet right after the workbook is opened.
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  11. #11
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    I would see this more as a case to use Worksheet_Activate to protect each individual worksheet, rather than unprotect it. Here's my take on the Pros/Cons vs just using the workbook_open route:

    Pros:
    -It would fire each time someone activate the worksheet which means that you could reprotect the worksheet if someone unprotected it and navigated away. (Similar to the Lock on Resume feature of a screensaver.)
    -You can easily control which sheets are subject to that feature... just drop the code in the ones you want to protect.
    -One place to look to see what is happening.

    Cons:
    -A new worksheet inserted in the file would not get the code inserted. This could be a bad thing if you want all sheets except the one you specified protected
    -Much more maintenance of code if you ever need to update something
    -Could really p!ss off a user if they unprotected the sheet to copy something in, went to another sheet and copied, then came back to paste it (the sheet would have been reprotected, right?) -- Actually, depending on how much you dislike your users, this could be a Pro.
    -Extra VBA overhead each time you flip sheets (granted not a lot, but some)
    -The Undo stack would be cleared every time you switched between worksheets in the workbook. (I don't think this is a good thing.)

    Long and short of it is that you need to know your goals. If you just want the sheets protected, go with the workbook_open routine. If you need any of the pros listed above, and can live with the cons, then go with the Worksheet_Activate event.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  12. #12
    VBAX Contributor
    Joined
    Mar 2007
    Posts
    140
    Location
    Thnks again Simon & Ken,

    The answer Ken wrote was what I was after... the Pros and Cons was a very helpfull explanation.
    I also appreciated Ken's down to earth type of answer.

    Regards,
    Dave T

Posting Permissions

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