+ Reply to Thread
Results 1 to 28 of 28

Protect - Unprotect Macro

  1. #1
    Forum Contributor jason.hampton's Avatar
    Join Date
    04-20-2012
    Location
    Republic of Texas
    MS-Off Ver
    2007, 2010, 2016
    Posts
    138

    Protect - Unprotect Macro

    All,

    Attached is a macro I've been using for years, and would like to add:
    1. Ability to use the 'data filters'
    2. use the 'grouping' commands.

    On various web pages, I've found syntax to perform additional steps 1 & 2. However, I've not been able to successfully compile them together.

    Any help would be appreciated.

    Protect-Unprotect.xlsm
    Best Regards,

    Jason Hampton
    Freedom Isn't Free

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Protect - Unprotect Macro

    Would you clarify what you mean please.

    The filter drop down and grouping functionality are in place and work.

    Arev you wanting to automate the appliation of some other grouping and to toggle the Autofilter?
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Contributor jason.hampton's Avatar
    Join Date
    04-20-2012
    Location
    Republic of Texas
    MS-Off Ver
    2007, 2010, 2016
    Posts
    138

    Re: Protect - Unprotect Macro

    Richard,

    They are working, but the workbook is unprotected. Click the button, and then input any password to verify. My apologies.

  4. #4
    Forum Contributor jason.hampton's Avatar
    Join Date
    04-20-2012
    Location
    Republic of Texas
    MS-Off Ver
    2007, 2010, 2016
    Posts
    138

    Re: Protect - Unprotect Macro

    Richard,

    Did you have any further questions?

  5. #5
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Protect - Unprotect Macro

    I'm still not clear what you're asking for.

    Are you saying that with protection on you can't use the filter?

    If so that's because when you set the password protection you need to tick the option that allows the filter to work

    i.e. in VBA

    Formula: copy to clipboard
    Please Login or Register  to view this content.

  6. #6
    Forum Contributor jason.hampton's Avatar
    Join Date
    04-20-2012
    Location
    Republic of Texas
    MS-Off Ver
    2007, 2010, 2016
    Posts
    138

    Re: Protect - Unprotect Macro

    Yes sir that is correct. When I protect the document, I can not 1. filter (or) 2. use grouping.

    I'd like to add this to the aforementioned VBA syntax; allowing filtering and grouping after protection.

    Thanks in advance.

  7. #7
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Protect - Unprotect Macro

    OK,
    There are two aspects here.
    1. Filtering has to be enabled when the sheet password is set.
    2. Using the Grouping functionality is not allowed when the worksheet is protected.

    To overcome #1 you should add that line I gave you last time to the CommandButton1 macro. i.e.
    Please Login or Register  to view this content.
    To overcome #2 you need to use some code in the Workbook Open event. i.e.

    Please Login or Register  to view this content.
    When a sheet is protected with UserInterfaceOnly in operation, the protection applies to stuff like keyboard clicks made by the user. VBA however is permitted to modify the worksheet as if there is no protection.
    The UserInterfaceOnly setting isn't saved when you close the workbook, so you need to set it when the workbook is opened. The best place to do this is in the Workbook_Open event procedure.

    Hope thsi helps.

  8. #8
    Forum Contributor jason.hampton's Avatar
    Join Date
    04-20-2012
    Location
    Republic of Texas
    MS-Off Ver
    2007, 2010, 2016
    Posts
    138

    Re: Protect - Unprotect Macro

    Richard,

    Thank you for your assistance. I've added the syntax to the CommandButton1. However when I run the macro it is now showing an error on the first line of Sub ShowPass() [indicated underline below]

    Please Login or Register  to view this content.
    Last edited by jason.hampton; 09-12-2019 at 09:24 AM.

  9. #9
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Protect - Unprotect Macro

    Re your PM to me.

    Please upload the workbook in the forum so that I may check.

  10. #10
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Protect - Unprotect Macro

    Your example file only has one sheet, not 2.
    Please Login or Register  to view this content.

  11. #11
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Protect - Unprotect Macro

    Quote Originally Posted by Kenneth Hobson View Post
    Your example file only has one sheet, not 2.
    Please Login or Register  to view this content.
    Hi Kenneth

    IIRC there was a second hidden sheet.

  12. #12
    Forum Contributor jason.hampton's Avatar
    Join Date
    04-20-2012
    Location
    Republic of Texas
    MS-Off Ver
    2007, 2010, 2016
    Posts
    138

    Re: Protect - Unprotect Macro

    Richard,

    As requested, attached is my actual workbook I am having difficulties with..
    Last edited by jason.hampton; 09-13-2019 at 11:11 AM.

  13. #13
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Protect - Unprotect Macro

    What's the password?

  14. #14
    Forum Contributor jason.hampton's Avatar
    Join Date
    04-20-2012
    Location
    Republic of Texas
    MS-Off Ver
    2007, 2010, 2016
    Posts
    138

    Re: Protect - Unprotect Macro

    I pm'd the password to you.

  15. #15
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Protect - Unprotect Macro

    Kenneth was correct.

    Although your previous workbook had two sheets - which prompted my response to Kenneth, this latest one only has one sheet.

    Hence your code
    Please Login or Register  to view this content.
    which is looking for the second sheet tab fails.

    You should avoide using code like Sheets(2) or Sheets("TimeLine").
    The order of sheet tab names can easily be changed so that the sheet which VBA knows as Sheets(1) for instance will become Sheets(3) if it's tab name is moved to be the third sheet. Similarly sheet tab names are too easily changed so if you hard code these things you are creating a potential problem

    Always use the VBA Sheet Code name. In this case Sheet1. This stays consistent whatever the user does in the Excel App.

  16. #16
    Forum Contributor jason.hampton's Avatar
    Join Date
    04-20-2012
    Location
    Republic of Texas
    MS-Off Ver
    2007, 2010, 2016
    Posts
    138

    Re: Protect - Unprotect Macro

    Quote Originally Posted by Richard Buttrey View Post
    Kenneth was correct.

    Although your previous workbook had two sheets - which prompted my response to Kenneth, this latest one only has one sheet.

    Hence your code
    Please Login or Register  to view this content.
    which is looking for the second sheet tab fails.

    You should avoide using code like Sheets(2) or Sheets("TimeLine").
    The order of sheet tab names can easily be changed so that the sheet which VBA knows as Sheets(1) for instance will become Sheets(3) if it's tab name is moved to be the third sheet. Similarly sheet tab names are too easily changed so if you hard code these things you are creating a potential problem

    Always use the VBA Sheet Code name. In this case Sheet1. This stays consistent whatever the user does in the Excel App.

    Thank you! I corrected the code to Sheet1.

    However, now I am not able to use the grouping options...

  17. #17
    Forum Contributor jason.hampton's Avatar
    Join Date
    04-20-2012
    Location
    Republic of Texas
    MS-Off Ver
    2007, 2010, 2016
    Posts
    138

    Re: Protect - Unprotect Macro

    Quote Originally Posted by Kenneth Hobson View Post
    Your example file only has one sheet, not 2.
    Please Login or Register  to view this content.
    Kenneth,

    Thank you sir!

  18. #18
    Forum Contributor jason.hampton's Avatar
    Join Date
    04-20-2012
    Location
    Republic of Texas
    MS-Off Ver
    2007, 2010, 2016
    Posts
    138

    Re: Protect - Unprotect Macro

    When I protect the workbook now the filtering works great, however the grouping is inoperable. Any ideas?

  19. #19
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Protect - Unprotect Macro

    I have not used the ProtectContents. I normally use Sheet's Protect as Richard did in 2nd part of #7.

    Of course there are other sheet protect options that you can use besides UserInterFaceOnly:=True which allows macros to make changes. Record a sheet protect operation to see if an option gives you the flexibility that you need.

  20. #20
    Forum Contributor jason.hampton's Avatar
    Join Date
    04-20-2012
    Location
    Republic of Texas
    MS-Off Ver
    2007, 2010, 2016
    Posts
    138

    Re: Protect - Unprotect Macro

    Thank you sir. I will see what I can come up with and advise the group.

  21. #21
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Protect - Unprotect Macro

    Hi,

    Re your PM to me and comments here.

    As I understand it the Grouping functionality will not work when Workbook Protection is in place.
    I'll have a look further later.

  22. #22
    Forum Contributor jason.hampton's Avatar
    Join Date
    04-20-2012
    Location
    Republic of Texas
    MS-Off Ver
    2007, 2010, 2016
    Posts
    138

    Re: Protect - Unprotect Macro

    Richard,

    Thank you! This is beyond my skill set... (Yesterday I purchased the Excel 2016 Bible - specifically for the VBA section) Any other recommended reference publications?

  23. #23
    Forum Contributor jason.hampton's Avatar
    Join Date
    04-20-2012
    Location
    Republic of Texas
    MS-Off Ver
    2007, 2010, 2016
    Posts
    138

    Re: Protect - Unprotect Macro

    Richard / Kenneth,

    I was able to develop the syntax for a sub function while opening. This seems to work well. Is this the most efficient way to program the macro?

    Please Login or Register  to view this content.
    Last edited by jason.hampton; 09-13-2019 at 11:31 AM.

  24. #24
    Forum Contributor jason.hampton's Avatar
    Join Date
    04-20-2012
    Location
    Republic of Texas
    MS-Off Ver
    2007, 2010, 2016
    Posts
    138

    Re: Protect - Unprotect Macro

    Well... I take that back. Saved, closed and reopened the workbook. It doesn't allow for grouping.

    The search continues..

  25. #25
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Protect - Unprotect Macro

    Not a definitive answer since I've not delved in again but I did notice yesterday that when I unlocked every cell, and I mean every cell, I seem to remember that allowed the Grouping functionality to work. But that might have been becuase the workbook was not protected.

    Did you add the workbook protection to the last file you uploaded where it wasn't present before by any chance?

    If that's the case and you still need to stop users selecting cells where there's a formula, then a SheetChange event macro that detected the use was selecting a formula cell could be used to stop them.

  26. #26
    Forum Contributor jason.hampton's Avatar
    Join Date
    04-20-2012
    Location
    Republic of Texas
    MS-Off Ver
    2007, 2010, 2016
    Posts
    138

    Re: Protect - Unprotect Macro

    No sir, I did not upload a new revision of the document. Preferably, I only want users to input data in C6:G132 to ensure formulas and conditional formatting are not hampered with.

  27. #27
    Forum Contributor jason.hampton's Avatar
    Join Date
    04-20-2012
    Location
    Republic of Texas
    MS-Off Ver
    2007, 2010, 2016
    Posts
    138

    Re: Protect - Unprotect Macro

    And be allowed to filter C6:G6, along with the grouping in the left pane.

  28. #28
    Forum Contributor jason.hampton's Avatar
    Join Date
    04-20-2012
    Location
    Republic of Texas
    MS-Off Ver
    2007, 2010, 2016
    Posts
    138

    Re: Protect - Unprotect Macro

    Seem to have this coding issue resolved. Thanks for the support.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Macro to Unprotect and Protect
    By MBeedle in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-22-2013, 01:32 AM
  2. [SOLVED] Protect / Unprotect Macro
    By jason.hampton in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-18-2012, 11:53 AM
  3. [SOLVED] Protect / Unprotect Macro
    By jason.hampton in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 06-06-2012, 03:36 AM
  4. unprotect and protect macro
    By stevekirk in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-26-2007, 05:32 AM
  5. Macro to Protect and UnProtect
    By mmacw in forum Excel General
    Replies: 2
    Last Post: 02-13-2007, 04:39 PM
  6. protect / unprotect VBA project by macro
    By sylvain in forum Excel General
    Replies: 0
    Last Post: 07-31-2006, 01:13 PM
  7. [SOLVED] unprotect and protect within a macro
    By Staalkop in forum Excel General
    Replies: 4
    Last Post: 10-27-2005, 04:05 PM
  8. Macro to protect/unprotect with password
    By Cam in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-26-2005, 06:05 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1