Option Explicit
Dim ProtectArray(15) As Variant
Dim BoolProtect As Boolean
Const pw As String = "test"
Sub UnprotectSheet()
Dim ws As Worksheet
Set ws = ActiveSheet
BoolProtect = False
' Checks if the sheet is protected
If ws.ProtectContents = True Or ws.ProtectDrawingObjects = True Or ws.ProtectScenarios = True Then
' Save the protection properties
ProtectArray(0) = ws.ProtectDrawingObjects
ProtectArray(1) = ws.ProtectContents
ProtectArray(2) = ws.ProtectScenarios
ProtectArray(3) = ws.ProtectionMode
ProtectArray(4) = ws.Protection.AllowFormattingCells
ProtectArray(5) = ws.Protection.AllowFormattingColumns
ProtectArray(6) = ws.Protection.AllowDeletingRows
ProtectArray(7) = ws.Protection.AllowInsertingColumns
ProtectArray(8) = ws.Protection.AllowInsertingRows
ProtectArray(9) = ws.Protection.AllowInsertingHyperlinks
ProtectArray(10) = ws.Protection.AllowDeletingColumns
ProtectArray(11) = ws.Protection.AllowDeletingRows
ProtectArray(12) = ws.Protection.AllowSorting
ProtectArray(13) = ws.Protection.AllowFiltering
ProtectArray(14) = ws.Protection.AllowUsingPivotTables
BoolProtect = True
' Unprotect the worksheet
ws.Unprotect (pw)
End If
End Sub
Sub ProtectSheet()
Dim ws As Worksheet
Set ws = ActiveSheet
' Check if the sheet was protected
If BoolProtect = True Then
ws.Protect _
Password:=pw, _
DrawingObjects:=ProtectArray(0), _
Contents:=ProtectArray(1), _
Scenarios:=ProtectArray(2), _
UserInterfaceOnly:=ProtectArray(3), _
AllowFormattingCells:=ProtectArray(4), _
AllowFormattingColumns:=ProtectArray(5), _
AllowFormattingRows:=ProtectArray(6), _
AllowInsertingColumns:=ProtectArray(7), _
AllowInsertingRows:=ProtectArray(8), _
AllowInsertingHyperlinks:=ProtectArray(9), _
AllowDeletingColumns:=ProtectArray(10), _
AllowDeletingRows:=ProtectArray(11), _
AllowSorting:=ProtectArray(12), _
AllowFiltering:=ProtectArray(13), _
AllowUsingPivotTables:=ProtectArray(14)
BoolProtect = False
End If
End Sub