Delete all sheets that meets my critera without prompt

vlacombe

New Member
I'm looking for a script to delete any sheets that meets the following critera and without promtp:
If sheets contains the text: "PrixMax" "6po" "Quote" Then deleted them

Sheet name can be "PrixMax-250", "6po-Viny", "Quote-101018"
Along with those sheets, my workbook contains a "Master" sheets and "templates" which will remain

Thank you
 

offthelip

Well-known Member
try this code:
Code:
Dim nm As String
Dim Namesar(1 To 3) As String
Namesar(1) = "PrixMax"
Namesar(2) = "6po"
Namesar(3) = "Quote"
For i = 1 To Worksheets.Count
 nm = Worksheets(i).Name
   For j = 1 To 3
    testf = InStr(nm, Namesar(j))
    If testf <> 0 Then
      Application.DisplayAlerts = False
      Worksheets(i).Delete
      Application.DisplayAlerts = True
      Exit For
     End If
   Next j
Next i
 
Last edited:

vlacombe

New Member
it gives me error...
If I have 1 sheet to delete or none at all, it's fine
If I have 2 or more sheets that meets the criteria it may delete only 1, sometimes 2, then it gives me:

Run-time error "9"
Subscript out of range

and it's stuck at "nm = Worksheets(i).Name" when I enter the debug menu
 

offthelip

Well-known Member
I think that is because as the worksheets get deleted the worksheet count goes down so the loop goes too far, so we need to put acheck in at the end of the loop,
try this:
Code:
Dim nm As String

Dim Namesar(1 To 3) As String
Namesar(1) = "PrixMax"
Namesar(2) = "6po"
Namesar(3) = "Quote"
For i = 1 To Worksheets.Count
 nm = Worksheets(i).Name
   For j = 1 To 3
    testf = InStr(nm, Namesar(j))
    If testf <> 0 Then
      Application.DisplayAlerts = False
      Worksheets(i).Delete
      Application.DisplayAlerts = True
      Exit For
     End If
   Next j
if i= worksheets.count then
 exit for
end if
Next i
 
Last edited:

Fluff

MrExcel MVP, Moderator
Another option
Code:
Sub vlacombe()
   Dim Ary As Variant
   Dim Ws As Worksheet
   Dim i As Long
   
   Application.DisplayAlerts = False
   Ary = Array("PrixMax", "6po", "Quote")
   For Each Ws In Worksheets
      For i = 0 To UBound(Ary)
         If Ws.Name Like Ary(i) & "*" Then
            Ws.Delete
            Exit For
         End If
      Next i
   Next Ws
   Application.DisplayAlerts = True
End Sub
 

DanteAmor

Well-known Member
How about

Code:
Sub DeleteSeheets()
  Dim sh As Worksheet, n As String
  Application.DisplayAlerts = False
  For Each sh In Sheets
    n = LCase(sh.Name)
    If n Like LCase("*PrixMax*") Or n Like LCase("*6po-Viny*") Or n Like LCase("*Quote-101018*") Then
        sh.Delete
    End If
  Next
End Sub
 

vlacombe

New Member
Thanks all,

So many options! They all work now, I just picked one :)
I think Dante's version is missing the DisplayAlert = True at the end

Regards,
 

DanteAmor

Well-known Member
Thanks all,

So many options! They all work now, I just picked one :)
I think Dante's version is missing the DisplayAlert = True at the end

Regards,
Youre welcome and thanks for the feedback.

The line DisplayAlert = True at the end of the code is not necessary since the default value is True. Then when the code ends, the value returns to True.
 

vlacombe

New Member
Youre welcome and thanks for the feedback.

The line DisplayAlert = True at the end of the code is not necessary since the default value is True. Then when the code ends, the value returns to True.
Thanks for the info Dante, I'm learning everyday
Altho im sure that was probably basic stuff ;) hehehe
 

Some videos you may like

This Week's Hot Topics

  • Get External Data (long shot question!)
    This is likely a long shot but I am wondering if it is at all possible for Excel to somehow 'change' the contents of a URL that is being linked to...
  • Importing multiple excel files into one spreadsheet
    Hi, I'm trying to import multiple excel files (with the same format into a single spreadsheet) so that each day's file is listed underneath the...
  • Cell Formatting
    Good Morning, I need to format a few different cells in the following manners: A1 has to always add a colon (:) after whatever is typed in by a...
  • How to copy multiple rows using If
    Hi all, I'm very new to VBA and have written this simple code to copy certain cells if a certain cell within that row contains any data. I need...
  • Workbook_Change stopped working !
    I am working on an app to speed up & automate processing of Credit Cards statements. After data is input from a CSV file, it is presented to the...
  • VBA If statement
    Dear All, I have two dates, where I'd like a message box to pop, if the dates are between this criteria. [CODE] sDate1 = #10/1/2019#...
Top