Solved

Need a VBA macro to delete multiple sheets at once

Posted on 2013-11-06
4
6,386 Views
Last Modified: 2013-11-07
Hello,

I need your help with a VBA macro that would delete all sheets in a workbook except SheetA, SheetB, SheetC and SheetD.  Is this possible?
0
Comment
Question by:mldaigle1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 24

Accepted Solution

by:
Steve earned 84 total points
ID: 39627101
this should be OK...
Sub DeleteSheets()
Application.DisplayAlerts = False
Application.ScreenUpdating = False

For Each ws In Worksheets
    If ws.Name = "SheetA" _
        Or ws.Name = "SheetB" _
            Or ws.Name = "SheetC" _
                Or ws.Name = "SheetD" _
                    Then
                        'do nothing
                    Else
                        ws.Delete
    End If

Next ws
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

Open in new window

0
 
LVL 51

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 83 total points
ID: 39627127
Hi,

a shorter version of Barman's code
Sub DeleteSheets1()
Application.DisplayAlerts = False
Application.ScreenUpdating = False

For Each ws In Worksheets
    If ws.Name Like "Sheet[A-D]" Then
        'do nothing
    Else
    ws.Delete
    End If

Next ws
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

Open in new window

Regards
0
 
LVL 43

Assisted Solution

by:Saqib Husain, Syed
Saqib Husain, Syed earned 83 total points
ID: 39627395
If the macro can reside on a different workbook then you can do this without looping

Sub delshts()
    Set owb = ActiveWorkbook
    opath = owb.Path
    oname = owb.Name
    Sheets(Array("SheetA", "SheetB", "SheetC", "SheetD")).Move
    owb.Close False
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs opath & "/" & oname
    Application.DisplayAlerts = True
End Sub
0
 

Author Closing Comment

by:mldaigle1
ID: 39630813
Wow

I tested each of you solution the way you suggested and it work perfectly
Thanks!!
:)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question