+ Reply to Thread
Results 1 to 4 of 4

Delete all worksheets except the first one

Hybrid View

  1. #1
    Registered User
    Join Date
    09-20-2011
    Location
    Swiss
    MS-Off Ver
    Excel 2003
    Posts
    22

    Delete all worksheets except the first one

    Hi ,

    I have some xls files in C:\Prod_Dump folder, these xls have multiple worksheets, some xls files have 4 worksheets and some has 6 worksheets. I need to delete all worksheets except the first worksheet in all xls. Please give me some hint to do this.

    Thank you,
    Nithya

  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 all worksheets except the first one

    Hi Nithya1987, try
    Sub DeleteWorksheets()
        Dim Ws As Worksheet
        
        For Each Ws In Worksheets
            If Ws.Index <> 1 Then
                Application.DisplayAlerts = False
                Ws.Delete
                Application.DisplayAlerts = True
            End If
        Next Ws
    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 tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Delete all worksheets except the first one

    Nithya1987,

    Modification to Mordred's code to include all workbooks in the C:\Prod_Dump folder:
    Sub tgr()
        
        'Change path as necessary, don't forget the ending \
        Const strFldrPath As String = "C:\Prod_Dump\"
        
        Dim wb As Workbook
        Dim wsIndex As Long
        Dim CurrentFile As String
        
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
        
        CurrentFile = Dir(strFldrPath & "*.xls*")
        While CurrentFile <> vbNullString
            Set wb = Workbooks.Open(strFldrPath & CurrentFile)
            For wsIndex = wb.Sheets.Count To 2 Step -1
                wb.Sheets(wsIndex).Delete
            Next wsIndex
            wb.Close True
            CurrentFile = Dir
        Wend
        
        Application.DisplayAlerts = True
        Application.ScreenUpdating = True
        
    End Sub
    Hope that helps,
    ~tigeravatar

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

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

    Re: Delete all worksheets except the first one

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

+ 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