+ Reply to Thread
Results 1 to 6 of 6

Delete All Worksheets in a Workbook without the names

  1. #1
    Registered User
    Join Date
    03-06-2010
    Location
    Kuwait
    MS-Off Ver
    Excel 2007
    Posts
    2

    Delete All Worksheets in a Workbook without the names

    Hi,

    I'm not exactly a newbie to VBA or Visual Basic; however, I am having difficulty with making a part of a function delete all of the existing worksheets in a workbook despite their names.

    General techniques that I have tried:
    Please Login or Register  to view this content.
    In the example above, I've replaced sh with other variables and I've also tried "In Worksheets" in place of "ThisWorkbook.Sheets"

    When I try this, I get "Run-time error '1004': Delete method of Worksheet class failed"

    The code I used to create the sheet inside the workbook is:

    Please Login or Register  to view this content.
    (I tried changing "sh" to "sht" too - fyi)

    The function itself is quite lengthy so I left out the rest of the code... let me know if you need anything further to assist.

    Thanks,
    Dalcron

  2. #2
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264

    Re: Delete All Worksheets in a Workbook without the names

    A workbook must have a minimum of one sheet so I suspect you get that error when the last sheet is being deleted.
    Richard Schollar
    Microsoft MVP - Excel

  3. #3
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Delete All Worksheets in a Workbook without the names

    How far do you get before you get - "Run-time error '1004': Delete method of Worksheet class failed"

    Excel will not let you delete all the sheets in a book, it always retains one visible sheet.

    Why would you want to delete them all? Could you not just delete the workbook?

    Or am I missing something?

  4. #4
    Registered User
    Join Date
    03-06-2010
    Location
    Kuwait
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Delete All Worksheets in a Workbook without the names

    I'm deleting them all in process of inputing a new one. However, I want my code to stay with the workbook and not just create a new one.

    Thanks for this advice... I'll probably hide an unchanging worksheet that I need for later anyways, then I'll delete all but that one.

    Thanks!
    Greg

  5. #5
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264

    Re: Delete All Worksheets in a Workbook without the names

    The remaining sheet does need to be visible so bear that in mind when running the code.

  6. #6
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Delete All Worksheets in a Workbook without the names

    From Excel warning
    A workbook must contain at least one visible worksheet.
    To, hide, delete, or move the selected sheet(s),you must first insert a new sheet or unhide a sheet that is already hidden.
    Dalcron
    I'm deleting them all in process of inputing a new one. However, I want my code to stay with the workbook and not just create a new one.
    In that case why not delete all but one sheet
    Please Login or Register  to view this content.
    Rename the remaining sheet to make that the first worksheet in your new set

    You may have to add to the code to remove shapes controls charts etc from the last sheet

+ 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