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

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
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,215,028
Messages
6,122,753
Members
449,094
Latest member
dsharae57

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