sheet protection and a toggle button

ajm

Well-known Member
Joined
Feb 5, 2003
Messages
1,762
where does unprotect/protect go, and what is the correct syntax, when using a toggle button?

Code:
Private Sub ToggleButton1_Click()
If ToggleButton1.Value = False Then
  Range("13:35").EntireRow.Hidden = True
  ToggleButton1.Caption = "Display Summary"
Else
  Range("13:35").EntireRow.Hidden = False
  ToggleButton1.Caption = "Hide Summary"
End If
End Sub

is the same syntax used for a command button?
 

Some videos you may like

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

DominicB

Well-known Member
Joined
Oct 3, 2005
Messages
1,569
Good morning ajm

Something like this should be OK. You have to remember that once the sheet is protected, it must be unprotected before you can unhide the rows, hence you protect at the end of the If statement and unprotect at the start of the else statement :

Code:
Private Sub ToggleButton1_Click()
If ToggleButton1.Value = False Then
  Range("13:35").EntireRow.Hidden = True
  ToggleButton1.Caption = "Display Summary"
  ActiveSheet.Protect
Else
  ActiveSheet.Unprotect
  Range("13:35").EntireRow.Hidden = False
  ToggleButton1.Caption = "Hide Summary"
End If
End Sub

HTH

DominicB
 

ajm

Well-known Member
Joined
Feb 5, 2003
Messages
1,762
will the sheet still be protected when the user has the summary rows unhidden? my thinking is thus: at first when they view the sheet with the button upon it, the won't be able to see the summary. the button wil say "Display Summary" and the sheet is protected. On clicking the button, the sheet is unprotected, the rows are unhidden, and the caption changes to "Hide Summary". Doesn't this leave the sheet unprotected at this point?
 

DominicB

Well-known Member
Joined
Oct 3, 2005
Messages
1,569
Hi ajm

Doesn't this leave the sheet unprotected at this point?

Certainly will, but your post wasn't that specific about what you wanted. Now I have a better understanding, try this :

Code:
Private Sub ToggleButton1_Click()
If ToggleButton1.Value = False Then
ActiveSheet.Unprotect
Range("13:35").EntireRow.Hidden = True
ToggleButton1.Caption = "Display Summary"
ActiveSheet.Protect
Else
ActiveSheet.Unprotect
Range("13:35").EntireRow.Hidden = False
ToggleButton1.Caption = "Hide Summary"
ActiveSheet.Protect
End If
End Sub

HTH

DominicB
 

Watch MrExcel Video

Forum statistics

Threads
1,113,798
Messages
5,544,343
Members
410,604
Latest member
jeffamore
Top