I am trying to simply turn the protection on and off using VBA. The code I use is ACTIVESHEET.PROTECT and ACTIVESHEET.UNPROTECT. However, this command keeps returning errors when I run it from a toggle button.
Runtime error '1004'
Unprotect method of worksheet class failed
All I am trying to do is show/hide a few rows but I need to keep protection switched on. I have not used a password so I can't see why it doesn't work. The strange thing is once it fails you can't turn the protection on or off without closing the workbook down and re-opening.
The code I used was:
Private Sub tgbStatus_Click()
ActiveSheet.Unprotect
If tgbStatus = True Then
Rows("4:6".Select
Selection.EntireRow.Hidden = True
tgbStatus.Caption = "Show Status"
Range("A9".Select
Else
Rows("4:6".Select
Selection.EntireRow.Hidden = False
tgbStatus.Caption = "Hide Status"
Range("A9".Select
End If
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End Sub
Runtime error '1004'
Unprotect method of worksheet class failed
All I am trying to do is show/hide a few rows but I need to keep protection switched on. I have not used a password so I can't see why it doesn't work. The strange thing is once it fails you can't turn the protection on or off without closing the workbook down and re-opening.
The code I used was:
Private Sub tgbStatus_Click()
ActiveSheet.Unprotect
If tgbStatus = True Then
Rows("4:6".Select
Selection.EntireRow.Hidden = True
tgbStatus.Caption = "Show Status"
Range("A9".Select
Else
Rows("4:6".Select
Selection.EntireRow.Hidden = False
tgbStatus.Caption = "Hide Status"
Range("A9".Select
End If
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End Sub