VBA to perform multiple actions depending on checkbox clicks

brunette

Board Regular
Joined
Aug 19, 2003
Messages
97
Hi everyone,

I have various sheets in one workbook. Sheet 1 is the "summary sheet" where I have all my checkboxes. What I am after is, depending on what checkboxes are ticked, certain actions should happen. For each checkbox, I need 2 actions to occur simultaneously. I need a sheet unhidden and, on another sheet, certain rows unhidden (in both cases the default setting is hidden).

This leads me to ask 2 questions:

1) The code I have managed to piece together only works in part, insofar that I can get it to unhide the sheet I want but it isn't unhiding the rows I want on the second sheet. Can you help me as I can't see where I'm going wrong.
2) Will this code work if I select multiple checkboxes at the same time?

The checkbox is ActiveX

Code:
Private Sub CheckBox1_Click()
    On Error Resume Next
    ThisWorkbook.Sheets("Sheet2").Visible = CheckBox1.Value
    If CheckBox1 = True Then
    ThisWorkbook.Sheets("Sheet3").Select
[5:10].EntireRow.Hidden = False
Else: [5:10].EntireRow.Hidden = True
End If
       
End Sub

Thanks for your assistance
B
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Untested, try:
Code:
Private Sub CheckBox1_Click()
      
    On Error Resume Next
    Sheets("Sheet2").Visible = IIf(CheckBox1, xlVisible, xlSheetHidden)
    Sheets("Sheet3").Cells(5, 1).Resize(6).EntireRow.Hidden = Not CheckBox1
    On Error GoTo 0
   
End Sub
If this works, then to generalise for all checkboxes, (also untested) try:
Code:
Private Sub CheckBox1_Click()

    Click_CheckBox CheckBox1

End Sub

Public Sub Click_CheckBox(ByRef ChkBox As Variant)
'This can reside in a module
    On Error Resume Next
    Sheets("Sheet2").Visible = IIf(ChkBox, xlVisible, xlSheetHidden)
    Sheets("Sheet3").Cells(5, 1).Resize(6).EntireRow.Hidden = Not ChkBox
    On Error GoTo 0

End Sub
Unsure if code will work for multiple boxes, try and see!
 
Last edited:
Upvote 0
Hi JackDanIce,

Thanks so much for this.

It worked in part but I managed to tweak it so it works exactly the way I want it, so thank you!

Here's what I had to change for it to work properly:

Code:
Private Sub CheckBox1_Click()
      
    On Error Resume Next
    ThisWorkbook.Sheets("Sheet1").Visible = CheckBox1.Value
    Sheets("Sheet3").Cells(5, 1).Resize(6).EntireRow.Hidden = Not CheckBox1
    On Error GoTo 0
   
End Sub

From here, the rest worked perfectly too. Thanks again.
 
Upvote 0
You got it to work, even better and happy days! :)

PS you can probably drop the ThisWorkbook part, you don't pre-fix to Sheets("Sheets3"), which I assume is in the same workbook as Thisworkbook and your code works...
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,690
Members
449,117
Latest member
Aaagu

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