Easy way to delete Tabs

ERed1

Board Regular
Hi all,

I was just wondering if anyone new a macro that deleted specific tabs based on part their name. I have a macro that creates new tabs, but every tab has "Sheet 1" in it, the later tabs have "Sheet 1 (2)", "Sheet 1 (3)", etc. I just need those tabs to be deleted at the push of a button. I can explain more if someone needs it.

thanks in advance for the help
 

Fluff

MrExcel MVP, Moderator
How about
Code:
Sub ERed1()
   Dim ws As Worksheet
   Application.DisplayAlerts = False
   For Each ws In Worksheets
      If ws.Name Like "*(*)" Then ws.Delete
   Next ws
   Application.DisplayAlerts = True
End Sub
 

Joe4

MrExcel MVP, Junior Admin
This code will do that:
Code:
Sub DeleteSheets()
    Dim ws As Worksheet
    Application.DisplayAlerts = False
    For Each ws In Worksheets
        If Left(ws.Name, 7) = "Sheet 1" Then ws.Delete
    Next ws
    Application.DisplayAlerts = True
End Sub
 

ERed1

Board Regular
So I implemented this into my code, and I got it to delete "Sheet 1 (2)", but it isn't deleting "Sheet 1". So Here is what my code looks like with yours in it.
Code:
 Application.DisplayAlerts = False
   Sheets(Array("Merged", "Cat", Dog", "Plant", "Lizard", "Frog")). _    Select
    ActiveWindow.SelectedSheets.Delete
    
    Dim ws As Worksheet
    For Each ws In Worksheets
        If ws.Name Like "*(*)" Then ws.Delete
    Next ws


Application.DisplayAlerts = True
 
Last edited:

Joe4

MrExcel MVP, Junior Admin
Did you try my code?
I tested it out, and it will delete any Sheet name that starts with "Sheet 1".
 
Last edited:

Fluff

MrExcel MVP, Moderator
Try Joe's code, I misunderstood & thought you only wanted to delete the sheets with (1) etc, rather than all sheets that contained Sheet1 in the name
 

ERed1

Board Regular
So I tried both of your code, and Fluff's cleared the ones after, while Joe's cleared the first sheet, so I just put both of those in there and it works great that way.
 

Joe4

MrExcel MVP, Junior Admin
So I tried both of your code, and Fluff's cleared the ones after, while Joe's cleared the first sheet, so I just put both of those in there and it works great that way.
I created sheets named "Sheet 1", "Sheet 1 (2)", and "Sheet 1 (3)", like shown in your example, and it deleted all of them.
If my original code, as-is, did not do that for you, then I suspect that your sheets are really not named the way you first said in your original post.
 

ERed1

Board Regular
So I just noticed this; they are named "Sheet1". There isn't a space in there.
 

Fluff

MrExcel MVP, Moderator
In that case use
Code:
If Left(ws.Name, 6) = "Sheet1" Then ws.Delete
 
Last edited:

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