Delete all sheets that meets my critera without prompt

vlacombe

New Member
Joined
Oct 4, 2019
Messages
29
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
Joined
Dec 23, 2017
Messages
953
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
Joined
Oct 4, 2019
Messages
29
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
Joined
Dec 23, 2017
Messages
953
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
Joined
Jun 12, 2014
Messages
30,425
Office Version
365
Platform
Windows
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
Joined
Dec 3, 2018
Messages
7,854
Office Version
2007
Platform
Windows
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
Joined
Oct 4, 2019
Messages
29
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
Joined
Dec 3, 2018
Messages
7,854
Office Version
2007
Platform
Windows
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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
30,425
Office Version
365
Platform
Windows
Glad to help & thanks for the feedback
 

vlacombe

New Member
Joined
Oct 4, 2019
Messages
29
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
 

Forum statistics

Threads
1,078,243
Messages
5,339,048
Members
399,276
Latest member
BlueCheetah

Some videos you may like

This Week's Hot Topics

Top