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
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.
Just put this in the workbook before close event.
It will protect before closing .change the sheet and password as per your need![]()
Private Sub Workbook_BeforeClose(Cancel As Boolean) Sheet1.Protect Password:="1234" End Sub
Teach me Excel VBA
If you want to "toggle" between protected and not protected then:
And if you want to protect the sheet when you go to a different worksheet then:![]()
Sub ToggleProtect() If Sheets("Sheet1").ProtectContents = True Then Sheets("Sheet1").Unprotect Password = "PASSWORD" Else Sheets("Sheet1").Protect Password = "PASSWORD" End If End Sub
This goes in the sheet's module
And (for the sake of completeness) the code provided in post#2 above should be placed in the ThisWorkbook module![]()
Private Sub Worksheet_Deactivate() ActiveSheet.Protect Password = "PASSWORD" End Sub
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
Hi Imaran,
I am looking for toggle option - 1 key for both protect and unprotect. But will ask password when switch to unprotect.
or even
![]()
Sub ToggleProtect() If Sheets("Sheet1").ProtectContents = False Then Sheets("Sheet1").Protect Password = "PASSWORD" End Sub
Hi Kev,
Seems toggle option doesnt ask for password once unprotected.
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
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.
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.
What you need to do is to unprotect your sheet first, then run your code, then protect it again - like this:But I cannot run VBA script when protected
![]()
Sub DoSomething() Sheets("Sheet1").Unprotect Password = "PASSWORD" 'HERE you put your code that does something Sheets("Sheet1").Protect Password = "PASSWORD" End If
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.
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
My workbook full of VBA codes, Userforms. Very difficult to put the Password stament for each functions.
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
To unhide it via VBA![]()
Sheets("Sheet1").Visible = xlSheetVeryHidden
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.![]()
Sheets("Sheet1").Visible = True
Thanks Kev,
I successfully applied your code.
Glad it worked for you
Please mark thread as SOLVED (thread tools at top of thread)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks