Correct Syntax to Hide Multiple Sheets

Maverick99

Board Regular
Joined
Feb 9, 2008
Messages
108
I have over 60 sheets within a workbook. There are some sheets that I want to hide or unhide depending upon the macro. I have the MR and searched in several areas but keep coming up blank with how to either select or hide these sheets.

This is from the MR:

Code:
Sheets(Array("Process", "Utilities", "CodeRef", "DataRef (3)", "DataRef (2)", "DataRef", "Dept Summary New", "Summary_Dept", Summary_ Monthly")).Select

When I try to use this in the code it errors out. I'd appreciate any help with this.

Thanks,

Maverick
 
Erik,

I tried yours out and it errored at the .select part of the code.

I even started to get an error with JonMo's when there was one sheet left. When I inserted "On Error Resume Next" prior to the LBound and UBound statements, then it just left one sheet and carried on. I'll have to and manually delete the one sheet after performing actions on the rest.

Thanks for your input though,

Maverick
 
Upvote 0

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Also
If any sheet(s) is/are hidden, you can not Select.
Hi, jindon,

Long time ago we were in same thread :)
the code has
Code:
    For Each sht In ThisWorkbook.Sheets
    sht.Visible = True
    Next sht
so unless Maverick99 has changed the code, this should not be the problem

kind regards,
Erik
 
Upvote 0
Hi Erik,

It's been a while.
Right, I missed that part.
OP should show us the code that doesn't work.
 
Upvote 0
YES, jindon!!

Maverick99, you should try this out in a little sample workbook - no data needed - with "easy sheetnames", so you can check if you get it to work.

If not tell us what you are working on and what code you are using.

best regards,
Erik
 
Upvote 0
Erik,

Sorry it has taken so long to get back. This is the code that I tested in a new workbook.

Code:
Option Explicit
Sub test()
 
Dim sht As Variant
Dim aBudget As Variant
 
Application.ScreenUpdating = False
 
    For Each sht In ThisWorkbook.Sheets
    sht.Visible = True
    Next sht
 
aBudget = (Array("Sheet1 ", "Sheet2", "Sheet3 ", "Sheet4 "))
ThisWorkbook.Sheets(aBudget).Select
ActiveWindow.SelectedSheets.Visible = False
 
Application.ScreenUpdating = True
 
End Sub

I have 5 sheets 1-5. The sht.Visible does work. However, the aBudget provides the same Run-time error #9 (Subscript out of Range).

I have tried the array with and with the ( ) surrounding the entire statement. They both end up with the same result.

Do you have any ideas as to why this could be occuring?

Maverick
 
Upvote 0
Check the spelling of your sheet names, they need to be EXACT. Looks like you have trailing spaces on a couple of them...

aBudget = (Array("Sheet1 ", "Sheet2", "Sheet3 ", "Sheet4 "))
 
Upvote 0
JonMo,

You're right. I did have some trailing spaces and when fixed, it worked.

Thanks,

Maverick
 
Upvote 0
Hi can I find out how to hide an array of sheets that are open as well as all sheets except for 2 that are open? Eg. Sheet1 -Sheet3 are open. I want to hide them. I know I can easily record a macro to hide them but if subsequently Sheet4-Sheet5 are open can I assign the same macro to also hide them? How do I write the VBA macro code? I'm not very experienced in VBA so hope you will all be patient with my question. Thanks.
 
Upvote 0

Forum statistics

Threads
1,216,438
Messages
6,130,632
Members
449,584
Latest member
c_clark

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