ByVal Sub to Auto Hide sheets in a range

crburke92

Board Regular
Joined
Feb 5, 2019
Messages
71
Hey all,

Trying to build a form to easily clean up the amount of worksheets displayed based on a selection. Currently I have a Checkbox that has a cell reference of C2. C4:C10 will have worksheet names in them, and the adjacent cell D4:D10 has =IF(C2=TRUE,"Y","N"). When I select the checkbox, I want all sheets in the C column to be visible, and when deselected they will hide. I'll have multiple columns running similar code based off what sheets I want to see. My current code is:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
Dim SheetRef As Range
Dim TargetSheet As Worksheet
    
    If Range("C2") = "True" Then
        For Each SheetRef In Range("C4:C10")
            Set TargetSheet = ThisWorkbook.Worksheets(SheetRef.Offset(0, 1).Value)
            TargetSheet.Visible = (UCase(SheetRef.Value) = Y)
        Else
            For Each SheetRef In Range("C4:C10")
            Set TargetSheet = ThisWorkbook.Worksheets(SheetRef.Offset(0, 1).Value)
            TargetSheet.Hidden = (UCase(SheetRef.Value) = N)
    End If
    
End Sub

I have C4:C10 as Rly1:Rly10, and have sheets made with those names as well. Any help would be great as to why it's not updating!
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Change events are not triggered by formulae, as the cell has not changed.
Why not just assign a macro to your checkbox?
 
Upvote 0
Change events are not triggered by formulae, as the cell has not changed.
Why not just assign a macro to your checkbox?
Because that would have been to easy Fluffy! Also didn't know that formulas did not trigger.

It works now....

Thanks as always!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,004
Messages
6,122,659
Members
449,091
Latest member
peppernaut

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