+ Reply to Thread
Results 1 to 3 of 3

Deleting Sheets Based on Name

  1. #1
    The Boondock Saint
    Guest

    Deleting Sheets Based on Name

    Ive got a macro which makes a large number of sheets with information in
    them....

    Ive then got another macro which gets all the information from those
    sheets....

    the problem is... im then left with all the sheets.. and I really want to
    delete them.....

    Is there anyway to make a macro which would let me delete sheets.. based on
    their name.....

    all the ones i want to delete are called "cats (104)" or "cats
    (105)" etc etc..... there is one called "cats" which i want to keep.... so
    basically If it could someonie delete based on the term "cats ("

    any ideas?



  2. #2
    Registered User
    Join Date
    08-11-2005
    Location
    Netherlands Waddinxveen
    Posts
    81
    The important thing to know is to itarate from top to bottom to avoid problems due to the re-indexing of the worksheets by a worksheet delete

    in other words if you delete worksheets(3)
    worksheets(4) becomes worksheets(3)

    Please Login or Register  to view this content.
    I disn't test it but it should work

  3. #3
    Dave Peterson
    Guest

    Re: Deleting Sheets Based on Name

    One way:

    Option Explicit
    Sub testme()
    Dim wks As Worksheet
    Dim ErrCounter As Long

    ErrCounter = 0

    On Error Resume Next
    Application.DisplayAlerts = False
    For Each wks In ActiveWorkbook.Worksheets
    If LCase(wks.Name) Like "cats (*" Then
    wks.Delete
    If Err.Number <> 0 Then
    ErrCounter = ErrCounter + 1
    Err.Clear
    End If
    End If
    Next wks
    Application.DisplayAlerts = True
    On Error GoTo 0

    If ErrCounter > 0 Then
    MsgBox ErrCounter & " sheets were not deleted"
    End If

    End Sub



    The Boondock Saint wrote:
    >
    > Ive got a macro which makes a large number of sheets with information in
    > them....
    >
    > Ive then got another macro which gets all the information from those
    > sheets....
    >
    > the problem is... im then left with all the sheets.. and I really want to
    > delete them.....
    >
    > Is there anyway to make a macro which would let me delete sheets.. based on
    > their name.....
    >
    > all the ones i want to delete are called "cats (104)" or "cats
    > (105)" etc etc..... there is one called "cats" which i want to keep.... so
    > basically If it could someonie delete based on the term "cats ("
    >
    > any ideas?


    --

    Dave Peterson

+ 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