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
Solved

Need a VBA macro to delete multiple sheets at once

Posted on 2013-11-06
4
5,311 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
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 50

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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Outlook Free & Paid Tools
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 …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

790 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