Hide/unhide columns of multiple sheets using tickbox

bakarken

Board Regular
Joined
Sep 23, 2016
Messages
50
Office Version
  1. 2019
Platform
  1. Windows
Hello

I have a tickbox in Sheet1 and I would like a VBA that when this is ticked, columns H:M in ALL of Sheet2, Sheet3, Sheet4 and Sheet5 will hide. Then if this box is unchecked, they will reveal themselves.

I've seen examples where a VBA can apply to a single sheet, but is there a way it can be applied to multiple sheets?

Thanks
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hello. This code does as you ask, assuming the control is a form control and not Active X:

Paste this code in to a new module and assign this macro to the Check box

Code:
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
 
Upvote 0
Thank you for that gallen - should I be removing the 'Assuming line and 'record lines??
Sorry only have little experience using VBA's
 
Upvote 0
If you have an ActiveX checkbox, then in the programmer tab, select Mode design, right click on the checkbox and select to see code, in the panel paste the following code:

Code:
Private Sub CheckBox1_Click()
    shs = Array("sheet2", "sheet3", "sheet4", "sheet5")
    For i = LBound(shs) To UBound(shs)
        Sheets(shs(i)).Columns("H:M").EntireColumn.Hidden = CheckBox1
    Next
End Sub




When you click on checkbox automatically the coulums are hide / unhide on all the sheets
 
Upvote 0
Thank you for that gallen - should I be removing the 'Assuming line and 'record lines??
Sorry only have little experience using VBA's

You can do. Those lines are just comments. And have no effect on the compiled code when it runs.
 
Upvote 0
Dante I've come across a slight issue - hopefully you can help?

So the VBA was working but I've now decided I wish to lock sheets 2-5, such as rows 1-7 of each cannot be selected, edited etc. (rows 1-7 are locked, rows 8 onwards are unlocked).

I realise that hiding/unhiding locked cells will not work, so I have UNPROTECTED H1:M7, as these are the columns which I want to hide/unhide.

This is still not working when ticking the active box.

Is there a way that it can work?

To summarise: I wish to lock sheets 2-5 on A1:G7 and N1:AP7, and i wish a tickbox VBA to hide/unhide H:M (which are not locked/protected)

Thanks
 
Upvote 0
Try with this. change "abc" with your password

Code:
Private Sub CheckBox1_Click()
    shs = Array("sheet2", "sheet3", "sheet4", "sheet5")
    For i = LBound(shs) To UBound(shs)
        Sheets(CStr(shs(i))).Unprotect "abc"
        Sheets(CStr(shs(i))).Columns("H:M").EntireColumn.Hidden = CheckBox1
        Sheets(CStr(shs(i))).Protect "abc"
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,794
Messages
6,174,637
Members
452,575
Latest member
Fstick546

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