Macro to delete Hidden Worksheets?

jkharmer

Board Regular
Joined
Jul 7, 2008
Messages
77
Any ideas? It would be great of I could delete all but one of the hidden worksheets. It would be perfect if I could tell the macro not to delete one of the hidden worksheets (called "Test O2 and CO2"), but if that is too difficult, then just one to delete all the hidden sheets would be fine.

Any help would be much appreciated on this one,

James
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Please try this on a copy of your workbook.

Code:
Sub test()
Dim ws As Worksheet
Application.DisplayAlerts = False
For Each ws In ThisWorkbook.Worksheets
    If ws.Visible <> True And ws.Name <> "Test O2 and CO2" Then
        ws.Delete
    End If
Next ws
Application.DisplayAlerts = True
End Sub
 
Upvote 0
That is amazing! Thanks ever so much.

Trying to set up my own little business at the mo, and these macros will really help in the reporting template I am trying to put together!

James
 
Upvote 0
Sorry, just realised that the macro is still deleting the tab I do not want it to delete for some reason. It is now two tabs as well, not one - "Test O2" and "Test CO2". Any ideas on a modification to the code?

Thanks, James
 
Upvote 0
Try

Code:
Sub test()
Dim ws As Worksheet
Application.DisplayAlerts = False
For Each ws In ThisWorkbook.Worksheets
    If ws.Visible <> xlSheetVisible Then
        If ws.Name <> "Test O2" And ws.Name <> "Test CO2" Then
            ws.Delete
        End If
    End If
Next ws
Application.DisplayAlerts = True
End Sub
 
Upvote 0
I created a workbook containing several hidden sheets and ran the macro. It deleted every hidden sheet except for Test O2 and Test CO2 so I'm at a loss to understand why it didn't work for you. Sorry.
 
Upvote 0
Hi again VOG,

I will try to do the same in a new workbook - does it matter where I put the macro (it is not sitting in a module, but in one of my worksheet tabs (called "tests").

James
 
Upvote 0
Hi again VOG,

(it is not sitting in a module, but in one of my worksheet tabs (called "tests").

James

That's probably why it isn't working :)

Press ALT + F11 to open the Visual Basic Editor, Insert > Module and paste in the code. Then close the VBE and please try again. It should work with your existing workbook.
 
Upvote 0

Forum statistics

Threads
1,214,559
Messages
6,120,208
Members
448,951
Latest member
jennlynn

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