Hide/Unhide rows based on 2 checkboxes

thekamel

New Member
Joined
Feb 11, 2015
Messages
16
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
Hello,

I am trying to hide/unhide certain rows based on checkbox. I am using Form Control checkboxes. I am using the following VBA code for single checkboxes:

Sub CheckBox17_Click()
If ThisWorkbook.Sheets(1).CheckBoxes("Check Box 17").Value = 1 Then
ThisWorkbook.Sheets(1).Rows("34:40").Hidden = False
Else
ThisWorkbook.Sheets(1).Rows("34:40").Hidden = True
End If
End Sub

But now I need to do Rows 2-5 and 10-15 off the same checkbox. How can I combine the row to hide/unhide what I need?
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Since your macro is called CheckBox17_Click I presume that you are using a UserForm Checkbox so this is how your macro in the UserForm module should be, elsewise note the use of Union and adapt it to your case:
VBA Code:
Option Explicit
Private Sub CheckBox17_Click()
    Dim rng As Range
    With ThisWorkbook.Sheets(1)
        Set rng = Union(.Rows("34:40"), .Rows("2:5"), .Rows("10:15"))
        If UserForm1.CheckBox17.Value = True Then
            rng.EntireRow.Hidden = False
        Else
            rng.EntireRow.Hidden = True
        End If
    End With
End Sub
 
Upvote 0
Since your macro is called CheckBox17_Click I presume that you are using a UserForm Checkbox so this is how your macro in the UserForm module should be, elsewise note the use of Union and adapt it to your case:
VBA Code:
Option Explicit
Private Sub CheckBox17_Click()
    Dim rng As Range
    With ThisWorkbook.Sheets(1)
        Set rng = Union(.Rows("34:40"), .Rows("2:5"), .Rows("10:15"))
        If UserForm1.CheckBox17.Value = True Then
            rng.EntireRow.Hidden = False
        Else
            rng.EntireRow.Hidden = True
        End If
    End With
End Sub
When using the code, I am getting a "Run-time error 424: Object Required". The script I have is

Option Explicit
Private Sub CheckBox10_Click()
Dim rng As Range
With ThisWorkbook.Sheets(1)
Set rng = Union(.Rows("36:41"), .Rows("23:24"))
If UserForm1.CheckBox10.Value = True Then
rng.EntireRow.Hidden = False
Else
rng.EntireRow.Hidden = True
End If
End With
End Sub

I cannot figure out how to get it to function properly.
 
Upvote 0
How about
VBA Code:
Sub CheckBox17_Click()
   With ThisWorkbook.Sheets(1)
   Debug.Print .CheckBoxes("Check Box 17").Value
      .Rows("23:24").Hidden = .CheckBoxes("Check Box 17").Value <> 1
      .Rows("36:41").Hidden = .CheckBoxes("Check Box 17").Value <> 1
   End With
End Sub
 
Upvote 0
Solution
I cannot figure out how to get it to function properly.
Sorry, neither do I without seeing your file. Your code works for me as long that you have a form named "UserForm1" and a checkbox named "CheckBox10" in it.
 
Upvote 0
Yes Fluff, but as said in post #2 I wasn't so sure. Anyway, even without an userform I couldn't replicate error: "Run-time error 424: Object Required"
 
Upvote 0
You'll get that error if you do not have a userform called userform1 & you are not running Option Explicit
 
Upvote 0
Got it, that's why I always use Option Explicit :)(y).
 
Upvote 0

Forum statistics

Threads
1,214,431
Messages
6,119,462
Members
448,899
Latest member
maplemeadows

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