sheet protection and a toggle button

ajm

Well-known Member
Joined
Feb 5, 2003
Messages
2,004
Office Version
  1. 365
Platform
  1. Windows
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?
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
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
 
Upvote 0
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?
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,561
Messages
6,114,317
Members
448,564
Latest member
ED38

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top