IS Posible togle protection

  • Thread starter Thread starter purplemx via OfficeKB.com
  • Start date Start date
P

purplemx via OfficeKB.com

Hi

Hi have a file with password protection, but i need to know if is there a
code to VBA
to togle the protection ?

Please help me...
 
What kind of password protection?

Worksheet, workbook (both under Tools|Protection)
or
workbook (under File|SaveAs|tools)
or
the project (where macros live)?

Yes for all the answers except the project.
 
I have a sheet with protection
also i use a macro to protect the sheet and another one to unprotect
But i need to know if is posible a togle protection. Ineen only one macro to
unprotect and protect

Thanks

Dave said:
What kind of password protection?

Worksheet, workbook (both under Tools|Protection)
or
workbook (under File|SaveAs|tools)
or
the project (where macros live)?

Yes for all the answers except the project.
[quoted text clipped - 6 lines]
 
This protection is posible to do this with a one single macro
togle protection

Sub Protectsheet()
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End Sub

Sub Unprotect()

ActiveSheet.Unprotect
End Sub

With the first click Unprotect the sheet with the second one click protect
the codument
 
You can check to see if the worksheet is protected, then unprotect it. If it's
unprotected, then protect it.

But why not use two macros?
 
Since I have a lot of very large workbooks that take a long time to open
if I have the calculation set to automatic, I have a button on my
toolbar to run the following macro:

Sub ToggleCalculation()
Dim myCalc As Double, myMsg As String, myTitle As String, myMode As
String
Dim myStyle As Integer, myResponse As Integer
myCalc = Application.Calculation
Select Case myCalc
Case -4135
myMode = "Manual"
myMsg = "Calculation mode is currently " & myMode & vbCrLf
& _
"Do you want to change the " & vbCrLf & _
"calculation mode to Automatic?"
myStyle = vbYesNo + vbExclamation
myTitle = "Change Calculation Mode?"
myResponse = MsgBox(myMsg, myStyle, myTitle)
If myResponse = vbYes Then
Application.Calculation = xlAutomatic
End If
Case -4105
myMode = "Automatic"
myMsg = "Calculation mode is currently " & myMode & vbCrLf
& _
"Do you want to change the " & vbCrLf & _
"calculation mode to Manual?"
myStyle = vbYesNo + vbExclamation
myTitle = "Change Calculation Mode?"
myResponse = MsgBox(myMsg, myStyle, myTitle)
If myResponse = vbYes Then
Application.Calculation = xlManual
End If
End Select
End Sub

Can this not be modified to suit your purposes if you really want to
toggle?

I also have two macros for protecting and unprotecting all the sheets
in a workbook, but don't toggle these, just seperate buttons.

Sub ProtectAll()
Dim ws, t
ShowAllSheets
For Each ws In Worksheets
t = ws.Name
Sheets(t).Visible = True
Sheets(t).Select
ActiveSheet.Protect Password:="myPassWord"
Next ws
End Sub

Sub UnProtectAll()
Dim ws, t
ShowAllSheets
For Each ws In Worksheets
t = ws.Name
Sheets(t).Visible = True
Sheets(t).Select
ActiveSheet.Unprotect ("myPassWord")
Next ws
ActiveWorkbook.Unprotect ("myPassWord")
End Sub

Sub ShowAllSheets()
Dim sh As Object
For Each sh In ActiveWorkbook.Sheets
sh.Visible = True
Next sh
End Sub

The ShowAllSheets is necessary because the protect and unprotect macros
will crash if there is a hidden sheet.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top