+ Reply to Thread
Results 1 to 16 of 16

Toggle protection

Hybrid View

  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.
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Sheet1.Protect Password:="1234"
    End Sub
    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:
    Sub ToggleProtect()
        If Sheets("Sheet1").ProtectContents = True Then
            Sheets("Sheet1").Unprotect Password = "PASSWORD"    
        Else
            Sheets("Sheet1").Protect Password = "PASSWORD"
        End If
    End Sub
    And if you want to protect the sheet when you go to a different worksheet then:
    This goes in the sheet's module
    Private Sub Worksheet_Deactivate()
    ActiveSheet.Protect Password = "PASSWORD"
    End Sub
    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 Imaran,
    I am looking for toggle option - 1 key for both protect and unprotect. But will ask password when switch to unprotect.

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

    Re: Toggle protection

    or even
    Sub ToggleProtect()
        If Sheets("Sheet1").ProtectContents = False Then Sheets("Sheet1").Protect Password = "PASSWORD"
     
    End Sub

  6. #6
    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.

  7. #7
    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:
    Sub ToggleProtect()
        If Sheets("Sheet1").ProtectContents = True Then
     
        Else
            Sheets("Sheet1").Protect Password = "PASSWORD"
        End If
    End Sub

  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
    Sub ToggleProtect()
        Dim Pword As String
         If Sheets("Sheet1").ProtectContents = True Then
            Pword = InputBox("Enter a password")
            Sheets("Sheet1").Unprotect Password = Pword
            
          Else
            Sheets("Sheet1").Protect Password = "PASSWORD"
        End If
    
    End Sub
    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

    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:

    Sub DoSomething()
    
       Sheets("Sheet1").Unprotect Password = "PASSWORD"    
    
    'HERE you put your code that does something
       
       Sheets("Sheet1").Protect Password = "PASSWORD"
    
    End If

  11. #11
    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
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
      If Sheets("Sheet1").ProtectContents = False Then Sheets("Sheet1").Protect Password = "PASSWORD"
    End Sub
    Last edited by kev_; 04-22-2017 at 03:01 PM.

  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.
    Sub macroProtect()
    
    Worksheets("tracker").Protect Password:="abc", UserInterFaceOnly:=True
    
    End Sub

  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
    Sheets("Sheet1").Visible = xlSheetVeryHidden
    To unhide it via VBA
    Sheets("Sheet1").Visible = True
    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