Macro to hide multiple sheets

floridagunner

Board Regular
Joined
Jul 20, 2007
Messages
60
Hello,

I wrote a macro to select multiple sheets by name and hide them, but I keep getting the following error message:

"Object variable or with block variable not set".

Below is the macro:

Sub HideSheets1A()

Dim ws As Worksheet
Application.DisplayAlerts = False

If ws.Name = "Variance Evaluation" Or "Investment" Or "Costs & Incentives" Or "Revenues Total" Then ws.Visible = False

End Sub

I use Excel 2003

Can anyone point out where I'm going wrong?
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Try:

Code:
sheet1.display = xlsheetveryhidden 'repeat for all sheets

sheet1.display = true 'make sheet appear again

that should do it as long as you do it for all the sheets
 
Upvote 0
This version works (on hiding)

Code:
Sub HideSheets1A()

Dim ws As Worksheet
Application.DisplayAlerts = False

For Each ws In Sheets
If ws.Name = "Variance Evaluation" Or ws.Name = "Investment" Or ws.Name = "Costs & Incentives" Or ws.Name = "Revenues Total" Then ws.Visible = False

Next ws

End Sub
 
Upvote 0
Thank You Asala42, your code worked perfectly.

Bradgar, I'm sorry but I'm not sure what your code is doing but thanks anyway.
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

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