+ Reply to Thread
Results 1 to 3 of 3

VBA to delete sheet with certain name in all workbooks in a folder

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-18-2013
    Location
    Prague
    MS-Off Ver
    Excel 2013
    Posts
    161

    VBA to delete sheet with certain name in all workbooks in a folder

    Hi all,

    I'm trying to build a VBA which will delete a sheet with a predetermine name from each workbook in the selected folder.

    This code will be placed in a "Master Sheet" from which, when it will be started, will open one by one each worksheet in a given folder. If there is a sheet in the opened workbook with certain name (which will be filled in a list in the "Master Sheet"), the sheet will be deleted, and the workbook will be saved.

    I've start preparing the code, but it does not save teh file. Can you advise where do i make the mistake i it?

    Thank you.

    Igor

    Sub DeleteXSheetInAWorkbok()
    Application.DisplayAlerts = False
    
    DeleteThis = ActiveSheet.Range("A1")
    
    FileType = "*.xlsb*"
    With Application.FileDialog(msoFileDialogFolderPicker)
        .InitialFileName = ThisWorkbook.Path
        .AllowMultiSelect = False
        If .Show = -1 Then
            FilePath = .SelectedItems(1) & "\"
        Else
            Exit Sub
        End If
    End With
    
    Curr_File = Dir(FilePath & FileType)
    Do Until Curr_File = ""
    
        Set FldrWkbk = Workbooks.Open(FilePath & Curr_File, False, True)
        For Each Sheet In FldrWkbk.Sheets
            If Sheet.Name = DeleteThis Then
                Sheet.Delete
                FldrWkbk.Save
            End If
        Next Sheet
     
        FldrWkbk.Close
        Curr_File = Dir
    Loop
    
    Set FldrWkbk = Nothing
              Application.DisplayAlerts = True
    End Sub

  2. #2
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: VBA to delete sheet with certain name in all workbooks in a folder

    It works for me
    Sub DeleteXSheetInAWorkbok()
    Dim DeleteThis$, FilePath$, Curr_File$, FileType$
    Dim FldrWkbk As Workbook, wsh As Worksheet
    Application.DisplayAlerts = False
    
    DeleteThis = ActiveSheet.Range("A1")
    
    FileType = "*.xlsb*"
    With Application.FileDialog(msoFileDialogFolderPicker)
        .InitialFileName = ThisWorkbook.Path
        .AllowMultiSelect = False
        If .Show = -1 Then
            FilePath = .SelectedItems(1) & "\"
        Else
            Exit Sub
        End If
    End With
    
    Curr_File = Dir(FilePath & FileType)
    Do Until Curr_File = ""
    '    Set FldrWkbk = Workbooks.Open(FilePath & Curr_File, False, True)
        Set FldrWkbk = Workbooks.Open(Curr_File)
        For Each wsh In FldrWkbk.Sheets
            If wsh.Name = DeleteThis Then
                wsh.Delete
                FldrWkbk.Save
                Exit For
            End If
        Next wsh
    
        FldrWkbk.Close
        Curr_File = Dir
    Loop
    
    Set FldrWkbk = Nothing
    Application.DisplayAlerts = True
    End Sub
    Last edited by nilem; 11-14-2017 at 10:38 AM.

  3. #3
    Forum Contributor
    Join Date
    01-18-2013
    Location
    Prague
    MS-Off Ver
    Excel 2013
    Posts
    161

    Re: VBA to delete sheet with certain name in all workbooks in a folder

    спасибо nilem
    i've made samll adjustment and now it works

    Do Until Curr_File = ""
        Set FldrWkbk = Workbooks.Open(FilePath & Curr_File)
        For Each wsh In FldrWkbk.Sheets
            If wsh.Name = DeleteThis Then
                wsh.Delete
                FldrWkbk.Save
                Exit For
            End If
        Next wsh

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. One sheet to collate information from all workbooks in folder
    By JamesT1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-02-2015, 09:04 AM
  2. [SOLVED] Copy Same Sheet to Multiple Workbooks in Folder
    By allie14 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 04-02-2014, 10:13 AM
  3. [SOLVED] combine 1st sheet from all workbooks in folder
    By onbeillp111 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-18-2014, 05:14 PM
  4. [SOLVED] Delete A Worksheet From All Workbooks In A Folder
    By AlexRoberts in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-26-2011, 02:01 PM
  5. copy first sheet from all workbooks in folder
    By Steel Monkey in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-15-2011, 02:59 PM
  6. modify this code to delete a cell in all the given workbooks in a folder.
    By mrgillus in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-02-2009, 04:20 PM

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