Excel-VBA - Delete Worksheet

Posted on 2011-04-19
Last Modified: 2012-05-11
I have folder with 120 files.  Each file has two work sheets called DivRegion and Complex Report.  Is there a macro that can open all 120 files, delete those two specific worksheets and then save and close?
Question by:ArisaAnsar
    LVL 18

    Accepted Solution



    Sub kTest()
        Dim MyPath As String, MyFile As String
        Dim wbkActive As Workbook, wbkOpened As Workbook
        With Application
            .ScreenUpdating = 0
            .DisplayAlerts = 0
        End With
        Set wbkActive = ThisWorkbook
        MyPath = "C:\Users\"   'set your path here
        MyFile = Dir(MyPath & "*.xl*")
        Do While MyFile <> ""
            Set wbkOpened = Workbooks.Open(Filename:=MyPath & MyFile, UpdateLinks:=0)
            On Error Resume Next
            wbkOpened.Worksheets("Complex Report").Delete
            On Error GoTo 0
            wbkOpened.Close 1: Set wbkOpened = Nothing
            MyFile = Dir()
        With Application
            .ScreenUpdating = 1
            .DisplayAlerts = 1
        End With
    End Sub

    Open in new window


    Author Closing Comment

    Thank you! This worked great.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
    Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
    This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
    This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now