?
Solved

Excel-VBA - Delete Worksheet

Posted on 2011-04-19
2
Medium Priority
?
371 Views
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?
0
Comment
Question by:ArisaAnsar
2 Comments
 
LVL 18

Accepted Solution

by:
krishnakrkc earned 2000 total points
ID: 35426446
Hi,

Untested.

Kris
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("DivRegion").Delete
        wbkOpened.Worksheets("Complex Report").Delete
        On Error GoTo 0
        wbkOpened.Close 1: Set wbkOpened = Nothing
        MyFile = Dir()
    Loop
    
    With Application
        .ScreenUpdating = 1
        .DisplayAlerts = 1
    End With
End Sub

Open in new window

0
 

Author Closing Comment

by:ArisaAnsar
ID: 35426498
Thank you! This worked great.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

862 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