[RESOLVED] Delete all worksheets except few specified worksheets from a workbook
I have got the below macro to delete all worksheets in a workbook except few specified ones, but it shows type mismatch error on the line highlighted in bold. I am using excel 2010 version.
Code:
Sub deleteshts ()
Dim ws As Worksheet
Application.DisplayAlerts = False
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Sheet3" Or "Sheet8" Or "Sheet11" Or "Pivot" Then
ws.Delete
End If
Next
Application.DisplayAlerts = True
End Sub
Re: Delete all worksheets except few specified worksheets from a workbook
Code:
Sub delSheets()
Dim ws As Worksheet
Application.DisplayAlerts = False
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "pivot" And ws.Name <> "Sheet3" Then 'add more as needed
ws.Delete
End If
Next
Application.DisplayAlerts = True
End Sub
Re: Delete all worksheets except few specified worksheets from a workbook
of you could use a select case
Code:
Dim ws As Worksheet
Application.DisplayAlerts = False
For Each ws In ThisWorkbook.Worksheets
Select Case ws.Name
Case "sheet3", "pivot", "sheet8" ' add as required
' do nothing
Case Else
ws.Delete
End Select
Next
Application.DisplayAlerts = True
Re: Delete all worksheets except few specified worksheets from a workbook
Great. It works. thanks a lot :)