+ Reply to Thread
Results 1 to 3 of 3

Deleting all sheets except one

Hybrid View

  1. #1
    Nirmal Singh
    Guest

    Deleting all sheets except one

    I have a workbook in which I need to delete all sheets except one
    called "SummaryReport". I have tried using the following code

    For z = 1 To xlBook.Worksheets.Count
    If xlBook.Worksheets(z).Name <> "SummaryReport" Then
    xlBook.Worksheets(z).Delete
    Next z

    This is falling over (I presume because the count changes as sheets
    are deleted). How can I do this?

    Nirmal

  2. #2
    Nirmal Singh
    Guest

    Re: Deleting all sheets except one

    On Thu, 26 Jan 2006 15:54:14 +0000, Nirmal Singh
    <nirmal.singh@nottowerhamlets.notgov.uk> wrote:

    >I have a workbook in which I need to delete all sheets except one
    >called "SummaryReport". I have tried using the following code
    >
    >For z = 1 To xlBook.Worksheets.Count
    > If xlBook.Worksheets(z).Name <> "SummaryReport" Then
    >xlBook.Worksheets(z).Delete
    > Next z
    >
    >This is falling over (I presume because the count changes as sheets
    >are deleted). How can I do this?
    >
    >Nirmal


    I have changed the loop

    for z=xlBook.Worksheets.count to 1 step -1

    It works now.

    Nirmal

  3. #3
    Ron de Bruin
    Guest

    Re: Deleting all sheets except one

    You can use this

    For Each sh In ThisWorkbook.Worksheets

    If sh.Name <> "SummaryReport" Then


    --
    Regards Ron de Bruin
    http://www.rondebruin.nl


    "Nirmal Singh" <nirmal.singh@nottowerhamlets.notgov.uk> wrote in message news:lmrht1dqvtkoutd2bq0isf97c9ik58btdv@4ax.com...
    >I have a workbook in which I need to delete all sheets except one
    > called "SummaryReport". I have tried using the following code
    >
    > For z = 1 To xlBook.Worksheets.Count
    > If xlBook.Worksheets(z).Name <> "SummaryReport" Then
    > xlBook.Worksheets(z).Delete
    > Next z
    >
    > This is falling over (I presume because the count changes as sheets
    > are deleted). How can I do this?
    >
    > Nirmal




+ 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