+ Reply to Thread
Results 1 to 16 of 16

Toggle protection

  1. #1
    Registered User
    Join Date
    02-11-2017
    Location
    Singapore
    MS-Off Ver
    10
    Posts
    98

    Toggle protection

    Hi
    I am looking for a vba toggle interface protection with "password". With condition that upon closing the sheet will be auto protected (if not protected before closing).
    Thank you
    Regards,
    Ash
    Last edited by ash2017; 04-22-2017 at 01:37 PM.

  2. #2
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,785

    Re: Toggle protection

    Just put this in the workbook before close event.
    Please Login or Register  to view this content.
    It will protect before closing .change the sheet and password as per your need
    Teach me Excel VBA

  3. #3
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Toggle protection

    If you want to "toggle" between protected and not protected then:
    Please Login or Register  to view this content.
    And if you want to protect the sheet when you go to a different worksheet then:
    This goes in the sheet's module
    Please Login or Register  to view this content.
    And (for the sake of completeness) the code provided in post#2 above should be placed in the ThisWorkbook module
    Last edited by kev_; 04-22-2017 at 01:49 PM.
    Click *Add Reputation to thank those who helped you. Ask if anything is not clear

  4. #4
    Registered User
    Join Date
    02-11-2017
    Location
    Singapore
    MS-Off Ver
    10
    Posts
    98

    Re: Toggle protection

    Hi Kev,
    Seems toggle option doesnt ask for password once unprotected.

  5. #5
    Registered User
    Join Date
    02-11-2017
    Location
    Singapore
    MS-Off Ver
    10
    Posts
    98

    Re: Toggle protection

    Hi Imaran,
    I am looking for toggle option - 1 key for both protect and unprotect. But will ask password when switch to unprotect.

  6. #6
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Toggle protection

    If that is what you want try this:
    Please Login or Register  to view this content.

  7. #7
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Toggle protection

    or even
    Please Login or Register  to view this content.

  8. #8
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Toggle protection

    This toggles "protect" to ON if OFF and
    to OFF if ON, but only if correct password entered
    Please Login or Register  to view this content.
    Last edited by kev_; 04-22-2017 at 02:12 PM.

  9. #9
    Registered User
    Join Date
    02-11-2017
    Location
    Singapore
    MS-Off Ver
    10
    Posts
    98

    Re: Toggle protection

    Hi Kev, the last option is what I am looking for. But I cannot run VBA script when protected. Is there any interface protection with similar function. Also upon closing can the sheet auto protected if not protected before closing.

  10. #10
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Toggle protection

    This must be placed in the ThisWorkbook module
    Please Login or Register  to view this content.
    Last edited by kev_; 04-22-2017 at 03:01 PM.

  11. #11
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Toggle protection

    But I cannot run VBA script when protected
    What you need to do is to unprotect your sheet first, then run your code, then protect it again - like this:

    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    02-11-2017
    Location
    Singapore
    MS-Off Ver
    10
    Posts
    98

    Re: Toggle protection

    Thanks Kev,
    The below code is working, the problem with re-protection. Thats why I am looking for toggle or auto protection on closing.
    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    02-11-2017
    Location
    Singapore
    MS-Off Ver
    10
    Posts
    98

    Re: Toggle protection

    My workbook full of VBA codes, Userforms. Very difficult to put the Password stament for each functions.

  14. #14
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Toggle protection

    Without seeing what the code does, it's difficult to advise. But if you want to protect your sheet, you have little choice.

    Does the user need to see the sheet?
    Instead of protecting the sheet you could hide it

    And there is one bit of code that stops user from unhiding the sheet without VBA
    Please Login or Register  to view this content.
    To unhide it via VBA
    Please Login or Register  to view this content.
    If you hide a sheet, you will not need to unhide it to run VBA - UNLESS your code is selecting the sheet or cells in the sheet - so you may need to amend your code avoid that.

  15. #15
    Registered User
    Join Date
    02-11-2017
    Location
    Singapore
    MS-Off Ver
    10
    Posts
    98

    Re: Toggle protection

    Thanks Kev,
    I successfully applied your code.

  16. #16
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Toggle protection

    Glad it worked for you
    Please mark thread as SOLVED (thread tools at top of thread)

+ 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. Toggle Button Does not Toggle OFF when Clicked
    By robbfamily in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-19-2014, 05:12 PM
  2. Replies: 1
    Last Post: 02-13-2014, 10:55 PM
  3. Button to toggle worksheet protection on/off?
    By Hambone70 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-05-2013, 05:32 PM
  4. Excel 2010 - Copying sheet with protection manually, protection stays... not with VBA
    By AdamParker in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-02-2012, 12:38 PM
  5. Toggle Worksheet Protection
    By Jazzzbo in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-12-2011, 04:50 PM
  6. Semi-Protection Want Hidability AND Protection
    By mbf1999 in forum Excel General
    Replies: 1
    Last Post: 11-17-2007, 01:24 PM
  7. [SOLVED] workbook Protection Toggle Macro
    By Just Learning in forum Excel General
    Replies: 3
    Last Post: 08-30-2005, 07: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