Results 1 to 4 of 4

Thread: How to delete a worksheet from multiple files

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Member mrmmickle1's Avatar
    Join Date
    Sep 2012
    Posts
    51
    Rep Power
    14
    Try something like this:

    Code:
    Sub EditFileDirectory()
    
        Dim path        As String
        Dim ThisWB      As String
        Dim Filename    As String
        Dim Wkb         As Workbook
        Dim lastR       As Long
        
        'Toggle off for speed
        Application.EnableEvents = False
        Application.ScreenUpdating = False
        
        On Error GoTo ErrorTrap
        ThisWB = ActiveWorkbook.Name
        path = "C:\Users\mmickle\Desktop\MyTest" 'Your File Directory Here
        Filename = Dir(path & "\*.xlsx", vbNormal)
        If Len(Filename) = 0 Then Exit Sub
        Do Until Filename = vbNullString
            If Not Filename = ThisWB Then
                Set Wkb = Workbooks.Open(Filename:=path & "\" & Filename)
                    Sheets(2).UsedRange.Copy 'Copy Data....<---- You may need to use ActiveWorkBook.Name to get your sheet name
                    Windows(ThisWB).Activate 'Activate Master WorkBook
                    lastR = Sheets("MasterCompilation").Range("A" & Rows.Count).End(xlUp).Row 'Define last row <---Change Sheet Name Accordingly
                    Range("A" & lastR + 1).PasteSpecial 'Paste on first open last row
                    Windows(Filename).Activate 'Re-activate File to clear data
                    Sheets(2).UsedRange.ClearContents 'clear data
                Wkb.Close True 'close file and save changes
            End If
            Filename = Dir() 'Go to next file in directory
        Loop
        
        'Toggle back on
        Application.EnableEvents = True
        Application.ScreenUpdating = True
        
        MsgBox "File editing is now complete", vbInformation, "ExcelFox- File Editor"
        On Error GoTo 0 'Reset Error Handling
        Exit Sub
        
    ErrorTrap:
        
        MsgBox "Error Num: " & Err.Number _
        & Chr(13) & "Error Desc: " & Err.Description _
        & Chr(13) & Chr(13) & "An unexpected error occured.  Please contact" _
        & Chr(13) & "File Administrator to help diagnose the issue." _
            , vbCritical, "ExcelFox- Error Handler"
        
    End Sub
    Last edited by mrmmickle1; 05-21-2015 at 03:46 AM.
    Using Excel 2010

  2. #2
    Junior Member
    Join Date
    Aug 2013
    Posts
    6
    Rep Power
    0

    Thank you

    That looks interesting. I'll have a go with it. For now I've found a very basic macro for deleting a sheet, saving and closing a workbook, that I can apply to all my files, once they are all open. This is saving me quite a lot of time already.
    Thank you for your helpful replies.

Similar Threads

  1. Moving Multiple Files From One Folder To Another
    By galang_ofel in forum Excel Help
    Replies: 5
    Last Post: 05-10-2013, 12:43 AM
  2. Loop Through And Delete Multiple File Types In A Folder
    By Excel Fox in forum Excel and VBA Tips and Tricks
    Replies: 0
    Last Post: 03-30-2013, 04:47 PM
  3. Replies: 4
    Last Post: 03-22-2013, 01:47 PM
  4. Saving and Running Macro For Multiple Files / Users
    By Charles_ in forum Excel Help
    Replies: 1
    Last Post: 01-07-2013, 09:10 AM
  5. Delete Names In A Specific Worksheet
    By Admin in forum Excel and VBA Tips and Tricks
    Replies: 0
    Last Post: 08-14-2011, 02:38 AM

Posting Permissions

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