Hide 2 worksheets

rspalding

Active Member
Joined
Sep 4, 2009
Messages
282
Office Version
  1. 365
Platform
  1. Windows
I'm using the following code for 1 sheet but I wan't to add a second sheet. I also want to hide Form 3 sheet. When I add it to this code, it errors.

Private Sub CheckBox1_Click()
On Error Resume Next
ThisWorkbook.Sheets("#UP FORM 3", "FORM 3").Visible = CheckBox1.Value
End Sub

Thanks,
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Code:
[COLOR=#333333]Private Sub CheckBox1_Click()[/COLOR]
[COLOR=#333333]On Error Resume Next[/COLOR]
[COLOR=#333333]ThisWorkbook.Sheets("#UP FORM 3").Visible = False
[/COLOR][COLOR=#333333]ThisWorkbook.Sheets("FORM 3").Visible = False[/COLOR]
[COLOR=#333333]End Sub[/COLOR]
 
Upvote 0
As long as the CheckBox1 is a boolean value, you can hide multiple sheets like so (CheckBox1 = False or 0):

Code:
ThisWorkbook.Sheets(Array("#UP FORM 3", "FORM 3")).Visible = CheckBox1.Value

The problem is though you can only unhide sheets one at a time. So you would need to loop through an array like so:

Code:
Option Explicit
Sub Macro1()

    Dim varMySheet As Variant
    
    Application.ScreenUpdating = False
    
    For Each varMySheet In Array("#UP FORM 3", "FORM 3")
        Sheets(varMySheet).Visible = CheckBox1.Value
    Next varMySheet
    
    Application.ScreenUpdating = True
    
End Sub

As such I would just use the array method as it's consistent and you wouldn't have to test whether you were hiding or unhiding to determine which method to use.

Regards,

Robert
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,588
Members
449,089
Latest member
Motoracer88

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