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
 

Some videos you may like

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
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
 

jkharmer

Board Regular
Joined
Jul 7, 2008
Messages
77
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
 

jkharmer

Board Regular
Joined
Jul 7, 2008
Messages
77
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
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650

ADVERTISEMENT

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
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650

ADVERTISEMENT

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.
 

jkharmer

Board Regular
Joined
Jul 7, 2008
Messages
77
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
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,123,258
Messages
5,600,568
Members
414,389
Latest member
MarkElla

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
Top