vba show or hide sheets

rhombus4

Well-known Member
Joined
May 26, 2010
Messages
586
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Not sure how 1st line will hide Sheet1 and Sheet3 if they are visible, but the second line won't show them. Also does it make a difference if I uses .Sheets or .Worksheets

ThisWorkbook.Sheets(Array("Sheet1", "Sheet3")).Visible = False 'Works if they are showing
ThisWorkbook.Sheets(Array("Sheet1", "Sheet3")).Visible = True 'Doesn't work

I know I can use code below but thought I could do it in one line

VBA Code:
Dim sh
For Each sh In Sheets(Array("Sheet1", "Sheet3"))
sh.Visible = True
Next
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Try this

VBA Code:
'hidden
Sheets(Array("Sheet1", "Sheet3")).Visible = xlSheetHidden
'visible
Sheets(Array("Sheet1", "Sheet3")).Visible = xlSheetVisible

Please IGNORE the above - just spotted that it does not work consistently
 
Last edited:
Upvote 0
1. A loop is necessary to make sheets VISIBLE - Excel does not appear to allow the array method above

2. If all sheets in the array are visible then this method works to HIDE sheets - but it fails if any of the sheets in the array are already hidden
Sheets(Array("Sheet1", "Sheet3")).Visible = xlSheetHidden
 
Upvote 0
Thanks, guess best way is the code below

Dim sh
For Each sh In Sheets(Array("Sheet1", "Sheet3"))
sh.Visible =
True
Next
 
Upvote 0
or ...
VBA Code:
    Dim sh As Variant
    For Each sh In Array("Sheet1", "Sheet3")
        Sheets(sh).Visible = True
    Next sh
 
Upvote 0
Thanks, although not sure what's the difference between the 2
 
Upvote 0

Forum statistics

Threads
1,215,430
Messages
6,124,849
Members
449,194
Latest member
HellScout

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