+ Reply to Thread
Results 1 to 5 of 5

How to delete multiple workshets

Hybrid View

  1. #1
    Registered User
    Join Date
    06-08-2011
    Location
    singapore
    MS-Off Ver
    Excel 2003
    Posts
    20

    How to delete multiple workshets

    Hi ,

    I have .xls files in a folder ( around 100+ excel files).each xls has 20 worksheets . I need to delete all the worksheets from all the xls files except the work sheet by name sheet1. is there a way I can do it in single shot,instead of doing it manually for each xls???

    Thx,Deepti

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: How to delete multiple workshets

    Deepti,

    This code will ask you to select a folder. Navigate to the desired folder and highlight it, then press "OK." It will then go through each excel file in that folder and if that file has a sheet named "Sheet1", it will delete all other sheets. At the end, the macro will let you know what workbooks did not have a sheet named "Sheet1".

    Here's the code:
    Sub WorksheetDeletionMacro_for_Deepti()
        
        Dim strFldrPath As String
        With Application.FileDialog(msoFileDialogFolderPicker)
            .AllowMultiSelect = False
            .Show
            On Error GoTo ExitSub
            strFldrPath = .SelectedItems(1)
        End With
    
        Dim CurrentFile As String: CurrentFile = Dir(strFldrPath & "\" & "*.xls")
        Dim SheetName As String:   SheetName = "Sheet1"
        Dim FailedWorkbooks As String: FailedWorkbooks = vbNullString
        Dim wb As Workbook, ws As Worksheet
        Application.ScreenUpdating = False
        While CurrentFile <> vbNullString
            Set wb = Workbooks.Open(strFldrPath & "\" & CurrentFile)
            If SheetExists(SheetName, wb) Then
                Application.DisplayAlerts = False
                For Each ws In wb.Sheets
                    If ws.Name <> SheetName Then ws.Delete
                Next ws
                Application.DisplayAlerts = True
            ElseIf FailedWorkbooks = vbNullString Then
                FailedWorkbooks = wb.Name
            Else
                FailedWorkbooks = FailedWorkbooks & ", " & wb.Name
            End If
            wb.Close True
            CurrentFile = Dir
        Wend
        Application.ScreenUpdating = True
        
        If FailedWorkbooks <> vbNullString Then MsgBox FailedWorkbooks & " did not have a sheet named " & SheetName & "."
        Exit Sub
        
    ExitSub:
        Exit Sub
        
    End Sub
    
    Private Function SheetExists(SheetName As String, wb As Workbook) As Boolean
        
        Dim wsCheck As Worksheet
        On Error GoTo NotFound
        Set wsCheck = wb.Sheets(SheetName)
        SheetExists = True
        Exit Function
        
    NotFound:
        SheetExists = False
        
    End Function


    Hope that helps,
    ~tigeravatar

  3. #3
    Registered User
    Join Date
    07-21-2012
    Location
    Prague, Czech
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: How to delete multiple workshets

    Hi Tigeravatar
    I have the opposite problem - how to write a macro that would delete certain spreadsheet across multiple workbooks (this spreadheet has the same name on almost 1000 identical workbooks I have). Can you please advice?
    Daniel

  4. #4
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: How to delete multiple workshets

    Daniel,

    Unfortunately, asking a question in the thread of another member is against the forum rules. As such, I have created a new thread for your question here:
    http://www.excelforum.com/excel-prog...html?p=2895807
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  5. #5
    Registered User
    Join Date
    07-21-2012
    Location
    Prague, Czech
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: How to delete multiple workshets

    Hi Tigeravatar,
    sorry for that! was not aware.
    Daniel

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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