+ Reply to Thread
Results 1 to 8 of 8

Delete A Worksheet From All Workbooks In A Folder

Hybrid View

  1. #1
    Registered User
    Join Date
    10-24-2010
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    49

    Delete A Worksheet From All Workbooks In A Folder

    I have been looking for a piece of code that will delete a worksheet from every workbook in a folder, but cannot find any examples.

    The worksheet may not exist, so if it does not the code should move onto the next workbook in the folder, no error messages are required.

    Can anybody help me.
    Last edited by AlexRoberts; 07-15-2011 at 06:44 AM.

  2. #2
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Delete A Worksheet From All Workbooks In A Folder

    Hi AlexRoberts, try
    Sub AlexRoberts()
        Dim ShtCnt As Long, ShtCntr As Long
        ShtCnt = ThisWorkbook.Sheets.Count
        For ShtCntr = 1 To ShtCnt
            If Sheets(ShtCntr).Name = "DeleteMe" Then
                Sheets(ShtCntr).Delete
            End If
        Next ShtCntr
    End Sub
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  3. #3
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Delete A Worksheet From All Workbooks In A Folder

    Here is code that will do that. It is well commented so you know what each line of code does. You need to change the folder path to your folder, and the sheet name to your sheet you are looking to delete.

    Public Sub DeleteWorksheets()
    
        'Declare local variables.
        Dim strCurPath As String
        Dim strFile As String
        Dim ws As Worksheet
        
        'Turn off screen updating so the code will run faster.
        Application.ScreenUpdating = False
        
    
        'Set up a dummy variable to hold the current path.
        strCurPath = "C:/YourFolder/"
        'Get the first file in the path.
        strFile = Dir(strCurPath & "*.xls")
            
        'Loop through each file in the directory.
        Do While strFile <> ""
            'Turn events off so we aren't asked to update links or no workbook.open macros are run.
            Application.EnableEvents = False
            'Open the workbook that is being examined.
            Workbooks.Open strCurPath & strFile, False
            'Turn events back on.
            Application.EnableEvents = True
            'Loop through each worksheet in the current workbook.
            For Each ws In ActiveWorkbook.Worksheets
                'Check if the worksheet is the one you want to delete.
                If ws.Name = "SheetName" Then
                    'Delete the sheet.
                    ws.Delete
                End If
            Next ws
            'Close the workbook.
            ActiveWorkbook.Close True
            'Go to the next file.
            strFile = Dir()
        Loop
        
        'Turn screen updating back on.
        Application.ScreenUpdating = True
    
    End Sub
    Last edited by davegugg; 07-13-2011 at 12:43 PM.
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  4. #4
    Registered User
    Join Date
    10-24-2010
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    49

    Re: Delete A Worksheet From All Workbooks In A Folder

    Dave,

    Thanks for this.

    A further question, it has just occurred to me that a spreadsheet may be open, is there a way to (a) ignore spreadsheets that are open and (b) list the file name of those open spreadsheets in another spreadsheet (log of some sort).

    Thank you.

  5. #5
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Delete A Worksheet From All Workbooks In A Folder

    Public Sub DeleteWorksheets()
    
        'Declare local variables.
        Dim strCurPath As String
        Dim strFile As String
        Dim ws As Worksheet
        
        'Turn off screen updating so the code will run faster.
        Application.ScreenUpdating = False
        
    
        'Set up a dummy variable to hold the current path.
        strCurPath = "C:/YourFolder/"
        'Get the first file in the path.
        strFile = Dir(strCurPath & "*.xls")
            
        'Loop through each file in the directory.
        Do While strFile <> ""
            'Turn events off so we aren't asked to update links or no workbook.open macros are run.
            Application.EnableEvents = False
            'Open the workbook that is being examined.
            Workbooks.Open strCurPath & strFile, False
            'Turn events back on.
            Application.EnableEvents = True
            'Check if the workbook opened as read only.
            If ActiveWorkbook.ReadOnly = True Then
                'If so, list it in the first available row in column A of sheet 1.
                ThisWorkbook.Sheets("Sheet1").Cells(ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row + 1, 1).Value = ActiveWorkbook.Name
                'Close the workbook without saving.
                ActiveWorkbook.Close False
            'If not opened in read only, search for worksheet to delete.
            Else
                'Loop through each worksheet in the current workbook.
                For Each ws In ActiveWorkbook.Worksheets
                    'Check if the worksheet is the one you want to delete.
                    If ws.Name = "SheetName" Then
                        'Delete the sheet.
                        ws.Delete
                    End If
                Next ws
                'Save and close the workbook.
                ActiveWorkbook.Close True
            End If
            'Go to the next file.
            strFile = Dir()
        Loop
        
        'Turn screen updating back on.
        Application.ScreenUpdating = True
    
    End Sub

  6. #6
    Registered User
    Join Date
    10-24-2010
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    49

    Re: Delete A Worksheet From All Workbooks In A Folder

    The time you have saved me, thank you.

  7. #7
    Registered User
    Join Date
    12-01-2008
    Location
    India
    MS-Off Ver
    Microsoft 365 Enterprise - Excel version2301
    Posts
    67

    Re: Delete A Worksheet From All Workbooks In A Folder

    Hi Davegugg , can you please amend code for me so that it unhide the particular hidden sheet and then delete it from all the workbooks in a folder?

    Thanks & regards,

    AG

  8. #8
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Delete A Worksheet From All Workbooks In A Folder

    If you have an issue Amarjeet, please read the forum rules (specifically # 2) and then start a new thread. You can post a link to this thread if you feel it contains relevant information.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1