Delete all sheets that meets my critera without prompt

vlacombe

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

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
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:
Upvote 0
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
 
Upvote 0
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:
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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,
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,212,933
Messages
6,110,751
Members
448,295
Latest member
Uzair Tahir Khan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top