Macro to hide sheets with specific names

floridagunner

Board Regular
Joined
Jul 20, 2007
Messages
60
Hello,

I need a macro that will hide sheet tabs with certain names as well as delete other sheets. However when it comes to processing the code where it has to check for sheet names and hide or leave alone, I am running into an error.

Below is the code I'm using:


Sub DeleteSheets2()

''Delete all worksheets except sheet 1

Dim ws As Worksheet
Application.DisplayAlerts = False
For Each ws In Worksheets
If ws.Name <> "Sheet1" And ws.Name <> "Variance Evaluation" And ws.Name <> "Analysis" And ws.Name <> "Device Removal Pivot Table" Then ws.Delete
Next
Application.DisplayAlerts = True
If ws.Name = "Variance Evaluation" Then ws.Visible = False
If ws.Name = "Analysis" Then ws.Visible = False
If ws.Name = "Device Removal Pivot Table" Then ws.Visible = False
''Add worksheets

Sheets("Sheet1").Select
Sheets.Add
ActiveSheet.Name = "Sheet3"
Sheets.Add
ActiveSheet.Name = "Sheet2"
Sheets("Sheet1").Select
Sheets("Sheet1").Move Before:=Sheets(1)

End Sub

Can anyone tell me what I'm doing wrong?

Thanks
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
What error on which line of code? Is the workbook structure protected?
 
Upvote 0
Hello VOG,

The workbook structure is not protected and the error occurs on the following line of code:

If ws.Name = "Variance Evaluation" Then ws.Visible = False

When I run the macro it processes the code above that line. Then the error message comes up with the following message:

"Run-time error '91':
Object Variable or With block variable not set"

This is followed by the option to press the "Debug" button.

I should also add that in this particular case the file does not have the sheets named "Variance Evaluation" or "Analysis". However I want the macro to check if sheets by that name exist, and if they do then the macro should hide them.

Thanks
 
Last edited:
Upvote 0
Try this

Code:
Sub DeleteSheets2()

''Delete all worksheets except sheet 1

Dim ws As Worksheet
Application.DisplayAlerts = False
For Each ws In Worksheets
    If ws.Name = "Variance Evaluation" Then ws.Visible = False
    If ws.Name = "Analysis" Then ws.Visible = False
    If ws.Name = "Device Removal Pivot Table" Then ws.Visible = False
    If ws.Name <> "Sheet1" And ws.Name <> "Variance Evaluation" And ws.Name <> "Analysis" And ws.Name <> "Device Removal Pivot Table" Then ws.Delete
Next
Application.DisplayAlerts = True
''Add worksheets

Sheets("Sheet1").Select
Sheets.Add
ActiveSheet.Name = "Sheet3"
Sheets.Add
ActiveSheet.Name = "Sheet2"
Sheets("Sheet1").Select
Sheets("Sheet1").Move Before:=Sheets(1)

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,826
Messages
6,121,792
Members
449,048
Latest member
greyangel23

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