Deleting Multiple Worksheets (or tabs) from a Workbook

jdurham67

New Member
Joined
May 13, 2011
Messages
8
I pull data daily and the resulting workbook contains multiple tabs. I always want to delete all of them except the one named "Master Data." The number of tabs can vary, but the one that I want to keep will always have the same name.

Thanks in advance!


running Excel 2007 on Windows 7



- Posting guidelines, forum rules and terms of use

- Try searching for your answer first, see how

- Read the FAQs

- List of BB codes
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
This macro will make sure the active workbook has the sheet, then it will delete all the others.
Code:
Option Explicit

Sub KeepOneWS()
Dim ws As Worksheet

Application.ScreenUpdating = False
Application.DisplayAlerts = False

With ActiveWorkbook
    If Evaluate("ISREF('Master Data'!A1)") Then
        For Each ws In ActiveWorkbook.Worksheets
            If ws.Name <> "Master Data" Then ws.Delete
        Next ws
    Else
        MsgBox "MASTER DATA sheet not found in active workbook."
    End If
End With

Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi

Say if you needed to keep 3 sheets in total, say Sheet1, Sheet2 and Sheet3, what would the code be?

Cheers

Jay
 
Upvote 0
Say if you needed to keep 3 sheets in total, say Sheet1, Sheet2 and Sheet3, what would the code be?

One way:

Code:
Option Explicit

Sub KeepSomeWS()
Dim ws As Worksheet
Dim ShtsKeep As String

ShtsKeep = "Sheet1,Sheet2,Sheet3"

Application.ScreenUpdating = False
Application.DisplayAlerts = False

    With ActiveWorkbook
        For Each ws In ActiveWorkbook.Worksheets
            If InStr(1, ws.Name, ShtsKeep) = 0 Then ws.Delete
        Next ws
    End With

Application.ScreenUpdating = True
End Sub

You might get unexpected leftovers if you were to keep, say, "Sheet11", a sheet called "Sheet1" would actually match and be kept. Hopefully you have better sheetnames than this.
 
Upvote 0
Hi

Tried it on 2003 and 2007 versions, it comes up with a runtime error 1004 method delete of object worksheet failed.

any other ideas?

Cheers

Jay
 
Upvote 0
Sorry, just got the parameters backwards...

Code:
Option Explicit

Sub KeepSomeWS()
Dim ws As Worksheet
Dim ShtsKeep As String

ShtsKeep = "Sheet1,Sheet2,Sheet3"

Application.ScreenUpdating = False
Application.DisplayAlerts = False

    With ActiveWorkbook
        For Each ws In ActiveWorkbook.Worksheets
            MsgBox InStr(1, ShtsKeep, ws.Name)
            'If InStr(1, ws.Name, ShtsKeep) = 0 Then ws.Delete
        Next ws
    End With

Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,574
Messages
6,179,628
Members
452,933
Latest member
patv

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