+ Reply to Thread
Results 1 to 9 of 9

Thread: Macro for deleting all worksheets after 1st three

  1. #1
    Registered User
    Join Date
    03-09-2011
    Location
    San Francisco , CA
    MS-Off Ver
    Excel 2003
    Posts
    8

    Exclamation Macro for deleting all worksheets after 1st three

    Hi Folks
    I want to just keep the 1st 3 worksheets in an excel sheet ( they are variably named e.g. 23,12,34 ....or 1,2,3 etc ) and delete the rest .
    Any pointers are appreciated.
    Thx
    S

  2. #2
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,640

    Re: Macro for deleting all worksheets after 1st three

    The problem that will arise is if the sheets are moved by someone
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  3. #3
    Registered User
    Join Date
    03-09-2011
    Location
    San Francisco , CA
    MS-Off Ver
    Excel 2003
    Posts
    8

    Exclamation Re: Macro for deleting all worksheets after 1st three

    Quote Originally Posted by royUK View Post
    The problem that will arise is if the sheets are moved by someone
    Hi Thx
    Not sure what is meant. I just wanted a simple macro, that can delete all worksheets from an Excel file , except the 1st three. The total # of work-sheets can vary but the deletes need to automatically happen after the 3rd worksheet. The 1st 3 worksheets can have variable names and not the standard sheet 1 , sheet 2 names

  4. #4
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,640

    Re: Macro for deleting all worksheets after 1st three

    If a user moves the position of any of the first three sheets say #2 to the fourth position or add sheets before sheet #1, this will change the sheets that are deleted

    Option Explicit
    
    Sub DeleteShts()
        Dim iX As Integer
        Dim Shts As Integer
    
        Shts = ThisWorkbook.Sheets.Count
        With Application
            .DisplayAlerts = False
            .ScreenUpdating = False
            On Error GoTo exit_proc
            For iX = Shts To 4 Step -1
                Sheets(iX).Delete
            Next iX
    exit_proc:
            .DisplayAlerts = True
            .ScreenUpdating = True
        End With
        On Error GoTo 0
    End Sub
    Last edited by royUK; 06-29-2011 at 03:13 AM.
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  5. #5
    Registered User
    Join Date
    06-29-2011
    Location
    California
    MS-Off Ver
    Excel 2003/2007
    Posts
    58

    Re: Macro for deleting all worksheets after 1st three

    You could use custom document properties to store the initial names of the sheets you want to keep and use something like...

    Sub DeleteSheets()
    wkscnt = ActiveWorkbook.Sheets.Count
    CheckSheets:
    If wkscnt > 0 Then
    If ActiveWorkbook.Sheets(wkscnt).Name = ActiveWorkbook.CustomDocumentProperties("SheetName1") _
    Or ActiveWorkbook.Sheets(wkscnt).Name =  ActiveWorkbook.CustomDocumentProperties("SheetName2") _
    Or ActiveWorkbook.Sheets(wkscnt).Name =  ActiveWorkbook.CustomDocumentProperties("SheetName3") Then
    wkscnt = wkscnt - 1
    Else
    Excel.Application.DisplayAlerts = False
    ActiveWorkbook.Sheets(wkscnt).Delete
    Excel.Application.DisplayAlerts = True
    End If
    GoTo CheckSheets
    End If
    
    End Sub
    You could also hide the sheet names somewhere on the spreadsheet and simply reference them using Cells(row value of cell, column value of cell).Value instead of custom document properties.

    If you want to know how to create custom document properties, just simply google it =)

    Good luck

    ~docMed

  6. #6
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Macro for deleting all worksheets after 1st three

    or
    sub snb()
      application.displayalerts=false
      do until thisworkbook.worksheets.count=3
        thisworkbook.worksheets(thisworkbook.worksheets.count).delete
      loop
    end sub
    Last edited by royUK; 06-29-2011 at 06:10 AM. Reason: amend code tags



  7. #7
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,640

    Re: Macro for deleting all worksheets after 1st three

    Storing the names wouldn't work if the user changed the names
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  8. #8
    Registered User
    Join Date
    06-29-2011
    Location
    California
    MS-Off Ver
    Excel 2003/2007
    Posts
    58

    Re: Macro for deleting all worksheets after 1st three

    Quote Originally Posted by royUK View Post
    Storing the names wouldn't work if the user changed the names
    you could use Workbook_NewSheet(ByVal Sh As Object) to detect up to 2 additional (since I'm going to assume there's at least 1 worksheet already) and capture their names by forcing them to name the worksheet any time a new sheet is created. Use a custom doc property integer to just count up to 3, and once that is satisfied, disable the workbook_newsheet command.

    Only issue is then how to capture additional name changes to the original worksheets...

    However, if the user simply created a worksheet each time, was prompted for the name immediately on each creation, and didn't change the name again, then... it could work - lol.

  9. #9
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,640

    Re: Macro for deleting all worksheets after 1st three

    The simplest way is to change the code name of the sheets that should be permanent then use them in the code
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

+ 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.2.0