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
 

offthelip

Well-known Member
Joined
Dec 23, 2017
Messages
1,030
Office Version
2010
Platform
Windows
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
31
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
1,030
Office Version
2010
Platform
Windows
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
33,381
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
9,531
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
31
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
9,531
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
33,381
Office Version
365
Platform
Windows
Glad to help & thanks for the feedback
 

vlacombe

New Member
Joined
Oct 4, 2019
Messages
31
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,084,748
Messages
5,379,616
Members
401,615
Latest member
syn_excel

Some videos you may like

This Week's Hot Topics

  • VBA code giving errors and stopping Excel
    Hello Experts, I have this code being used to loop through files in a file path, and copy specific data to another sheet. It is giving me several...
  • Disable MsgBox message
    Morning, I have a userform where if i leave a ComboBox empty i see a MsgBox warning me that i must enter an invoice number. It is this MsgBox i...
  • Macro Recorder into VBA, Copy Paste Data Filled Cells
    Hi Everyone, I have a macro recorder file that takes a selection of data, copies, then pastes into a new sheet on ("A2:B2") The issue is my...
  • Number format changes while pasting into a cell
    Hi, I am trying to paste a number 180204524303 from an email to an excel cell, however, whenever i try to do so , the the paste value appears as...
  • Collating data
    Hello all. Could someone please help. I am trying to pull all column data from multiple sheets (24 I total so far) into 1 master sheet without...
  • Sum Multiple Columns Based on Multiple Criteria
    I am trying to consolidate data by summing columns G through M based on material, plant, vendor, and fiscal year being identical. The period does...
Top