Hide/unhide columns of multiple sheets using tickbox - some locked cells

bakarken

Board Regular
Joined
Sep 23, 2016
Messages
50
Office Version
  1. 2019
Platform
  1. Windows
Hello I hope you all can help.

Sheet1 contains an activeX control (checkbox) which once ticked, columns H:M in all sheets sheet2, sheet3, sheet4, sheet5 will hide themselves (and when unticked, reveal themselves)

The VBA used (from another thread) is:
Sub ShowHideColumns()
'Assuming Checkbox 1 is a form control and named "Check Box 1"

Dim b As Boolean

'record if the control is ticked
b = Sheet1.Shapes("Check Box 1").ControlFormat.Value = 1

Sheet2.Range("H1:M1").EntireColumn.Hidden = b
Sheet3.Range("H1:M1").EntireColumn.Hidden = b
Sheet4.Range("H1:M1").EntireColumn.Hidden = b
Sheet5.Range("H1:M1").EntireColumn.Hidden = b

End Sub
Unfortunately, I would like to lock cells A1:G7, as well as N1:AP7 (protect cells and then protect sheet with password).

When I do so, the VBA no longer works (Despite the columns H:M not being locked, so I thought this would work)

Is there anyway I can make this work?

Thanks
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Using code tags makes your code easier to read (click on # icon above post window and paste your code between the tags which appear)

You need to Unprotect and re-protect each sheet before hiding or unhiding columns
Code assumes all sheets have the same password
Code:
Sub ShowHideColumns()
    Const pw = "[COLOR=#ff0000]YourPasswordGoesHere[/COLOR]"
    Dim ws As Variant, b As Boolean
    
    b = Sheet1.Shapes("Check Box 1").ControlFormat.Value = 1

    For Each ws In Array(Sheet2, Sheet3, Sheet4, Sheet5)
        With ws
            .Unprotect pw
            .Range("H1:M1").EntireColumn.Hidden = b
            .Protect pw
        End With
    Next
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,815
Messages
6,181,135
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