Deleting mutliple worksheets from a workbook if they conatin a specific tab name.

ruff

New Member
Joined
Jan 19, 2012
Messages
10
HI,

I have created the below macro to do this but it does not run through the whole workbook, I keep having to re-run the macro to delete other sheets (Some workbooks conatin 6-7 worksheets that end in Cube or Chart) Also If I could get it to stop asking me before deleting the worksheets and just delete them that would be great.


Sub delete_Cube_Chart_Tree()
For Each SheetExists In Worksheets
If Right(SheetExists.Name, 4) = "Cube" Then
SheetExists.Delete
Application.DisplayAlerts = True
Exit For
End If
Next SheetExists

For Each SheetExists In Worksheets
If Right(SheetExists.Name, 5) = "Chart" Then
SheetExists.Delete
Application.DisplayAlerts = True
Exit For
End If
Next SheetExists

For Each SheetExists In Worksheets
If Right(SheetExists.Name, 4) = "Tree" Then
SheetExists.Delete
Application.DisplayAlerts = True
Exit For
End If
Next SheetExists


End Sub
 

wigi

Well-known Member
Joined
Jul 10, 2006
Messages
7,944
Why do you use "Exit For", given that your loop stops too early?

Can you please use
Code:
 tags here? Thanks.
 

nuked

Well-known Member
Joined
Mar 20, 2013
Messages
883
Code:
 Application.DisplayAlerts = False
...prior to deleting the sheet would prevent the warning. Interestingly, you already have code that reverts it to True.
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Does this work for you?

Code:
Sub delete_Cube_Chart_Tree()
    Dim i As Long
    Application.DisplayAlerts = False
    For i = Worksheets.Count To 1 Step -1
        If Right(Worksheets(i).Name, 4) = "Cube" Or Right(Worksheets(i).Name, 5) = "Chart" Or Right(Worksheets(i).Name, 4) = "Tree" Then
            Worksheets(i).Delete
        End If
    Next i
    Application.DisplayAlerts = True
End Sub
You can't use For Each ... Next when deleting.
 

wigi

Well-known Member
Joined
Jul 10, 2006
Messages
7,944
Code:
You can't use For Each ... Next when deleting.[/QUOTE]

Hi Andrew

I set up an example workbook regarding this topic, and this code just works as expected:

[code]Sub delete_Cube_Chart_Tree()
    Application.DisplayAlerts = False
        For Each SheetExists In Worksheets
            If Right(SheetExists.Name, 5) = "chart" Then
                SheetExists.Delete
            End If
        Next
    Application.DisplayAlerts = True
End Sub
Can you maybe qualify your statement?
 

Forum statistics

Threads
1,081,752
Messages
5,361,085
Members
400,613
Latest member
Markdc123

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top