VBA Toggle Button on Protected Sheet

The_Steward

Board Regular
Joined
Nov 26, 2020
Messages
63
Office Version
  1. 365
Platform
  1. Windows
I am running this Macro to hide/unhide rows when form control button is clicked.

VBA Code:
Sub Important_Info_TOGGLE()
    Rows("24:42").EntireRow.Hidden = Not Rows("24:42").EntireRow.Hidden
End Sub

However when I protect the sheet it does not work. How can I fix this Macro so it runs when sheet is protected?

I had thought of using something like below but unsure how to configure correctly or if this would work.

VBA Code:
If Intersect(Target, OLEObject("CommandButton4")) Is Nothing Then Exit Sub
    ActiveSheet.Unprotect Password:=""
    With Sheets("Sheet19")
       Rows("24:42").EntireRow.Hidden = Not Rows("24:42").EntireRow.Hidden
    End With
    ActiveSheet.Protect Password:=""
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hello,

Your thinking is right.

The code below works - if the button is on the active worksheet.

VBA Code:
Sub HideRows()
ActiveSheet.UnProtect "PassWord"
Rows("24:42").EntireRow.Hidden = Not Rows("24:42").EntireRow.Hidden
ActiveSheet.Protect "PassWord"
End Sub

If you have several worksheets to unprotect; then protect: - and hide the rows, the below code does that.

VBA Code:
Sub UnProtect()
Dim ws As Worksheet
For Each ws In Worksheets
ws.UnProtect "PassWord"
Next ws
Rows("24:42").EntireRow.Hidden = Not Rows("24:42").EntireRow.Hidden
For Each ws In Worksheets
ws.Protect "PassWord"
Next ws
End Sub

Jamie
 
Upvote 0
Solution
Hello,

Your thinking is right.

The code below works - if the button is on the active worksheet.

VBA Code:
Sub HideRows()
ActiveSheet.UnProtect "PassWord"
Rows("24:42").EntireRow.Hidden = Not Rows("24:42").EntireRow.Hidden
ActiveSheet.Protect "PassWord"
End Sub

If you have several worksheets to unprotect; then protect: - and hide the rows, the below code does that.

VBA Code:
Sub UnProtect()
Dim ws As Worksheet
For Each ws In Worksheets
ws.UnProtect "PassWord"
Next ws
Rows("24:42").EntireRow.Hidden = Not Rows("24:42").EntireRow.Hidden
For Each ws In Worksheets
ws.Protect "PassWord"
Next ws
End Sub

Jamie
Thankyou Jamie! You are amazing :D

Didn't think it would be that simple,

Worked like a charm.
 
Upvote 0

Forum statistics

Threads
1,224,812
Messages
6,181,105
Members
453,021
Latest member
Justyna P

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