Consulting

Results 1 to 4 of 4

Thread: Delete all non-matching sheet names

  1. #1

    Arrow Delete all non-matching sheet names

    Workbook contain sheets as...
    FARES1, FARES2, RULES1, CONDITIONS1, RULES2, CONDITIONS2, SHEET_X, ROUTING1, ROUTING1 (2)

    FARES1 matches FARES2, RULES1 matches RULES2, CONDITIONS1 matches CONDITIONS2. Rest other sheets are non-matching sheets, which need to be deleted without prompt.

    note: sheet may contain any names and in any nos.

    msgbox=deleted sheet names and numbers of deleted sheet.

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Try this macro:


    Option Explicit
    
    Sub DelSheets()
    Dim DelSheet        As Collection
    Dim i               As Long
    Dim n               As Long
    Dim ws              As Worksheet
    Dim ws2             As Worksheet
    Dim Counter         As Long
    Dim Str             As String
    Application.DisplayAlerts = False
    Set DelSheet = New Collection
        Str = "The following sheets have been deleted:"
        For Each ws In Worksheets
            Counter = 0
            For Each ws2 In Worksheets
                If Left(ws2.Name, Len(ws2.Name) - 1) = Left(ws.Name, Len(ws.Name) - 1) Then
                    Counter = Counter + 1
                End If
            Next
            If Counter = 1 Then
                DelSheet.Add ws.Name
            End If
        Next
        If DelSheet.Count > 0 Then
            For i = 1 To DelSheet.Count
                Sheets(DelSheet(i)).Delete
                Str = Str & vbNewLine & DelSheet(i)
            Next
            MsgBox Str
        End If
    Application.DisplayAlerts = True
    End Sub

  3. #3
    Perfect.

    thankyou....

  4. #4
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    You're Welcome

    Take Care

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •