Delete Specific Sheets in a Workbook

Guzzlr

Well-known Member
Joined
Apr 20, 2009
Messages
982
Office Version
  1. 2021
Platform
  1. Windows
Code:
For Each sht In ActiveWorkbook.Worksheets
    If sht.Visible And (sht.Name = "4-0") _
    And (sht.Name = "4-1") _
    And (sht.Name = "4-2") Then
        sht.Delete
    End If
Next sht

Hello all
I thought the code above would loop through the workbook and delete the specific sheets listed, but it's not.
Where did I go wrong?
Thanks for the help
 
Question:
What if there is no 4-4 sheet, will the others still be deleted?
Thanks

PS. I'm still trying to figure out how to walk with one foot in front of the other...the variant idea is confusing to me. what is a variant?

No. If there is no sheet named 4-4 we would get a Error.

What is a variant?

I'm not able to explain every detail like this. Search internet for definition of Excel Variant.
I really do not understand some of these things I just know they work.
 
Upvote 0

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
A simple one:

Code:
Sub SheetsDelete()


   Worksheets(Array("4-0", "4-1", "4-2")).Delete
    
  End Sub
<strike></strike>
 
Upvote 0
Hi Guzzlr,

Seems like you want to delete all visible sheets that start with "4-". If this is correct the following will do the job (and be dynamic in that if you later add sheets that fit this rule set the code will delete those tab(s) again with any further tweaking):

Code:
Option Explicit
Sub Macro1()

    Dim sht As Worksheet
    
    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
    End With
    
    For Each sht In ThisWorkbook.Sheets
        'If the 'sht' sheet is visible and starts with 4- then...
        If sht.Visible = xlSheetVisible And Left(sht.Name, 2) = "4-" Then
            '...delete that sheet
            sht.Delete
        End If
    Next sht
    
    With Application
        .ScreenUpdating = True
        .DisplayAlerts = True
    End With

End Sub

Regards,

Robert
 
Upvote 0
A simple one:

Code:
Sub SheetsDelete()


   Worksheets(Array("4-0", "4-1", "4-2")).Delete
    
  End Sub
<strike></strike>

I knew something like this would work. But I forgot how to do it.
I do not use arrays very often but I need to do more of these so I don't forget how. My script used array the way I knew but is more work. This way is best.
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,212
Members
453,023
Latest member
alabaz

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