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
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
You want to delete what sheets?
Are you wanting to delete sheets named:
4-0
4-1
4-2

Delete those 3 sheets?
 
Upvote 0
You want to delete what sheets?
Are you wanting to delete sheets named:
4-0
4-1
4-2

Delete those 3 sheets?

Correct, only those sheets and nothing else. Also, two more I just added: 4-3 & 4-4

So. I wish to delete only sheets:

4-0
4-1
4-2
4-3
4-4

I have Dim sht as worksheet
is it because I have sht as worksheet, instead of worksheets
thanks
 
Last edited:
Upvote 0
Those "ANDs" in your original code need to be "ORs".
There is no way any sheet can be named two different things at once.
 
Upvote 0
How about
Code:
For Each Sht In ActiveWorkbook.Worksheets
    If Sht.Visible Then
      If Sht.Name = "4-0" Or Sht.Name = "4-1" Or Sht.Name = "4-2" Then
        Sht.Delete
      End If
    End If
Next Sht
 
Upvote 0
Try this:
Code:
Sub Delete_Sheets()
'Modified 3-7-18 4:00 PM EST
Application.ScreenUpdating = False
Dim i As Long
Dim nn As Long
Application.DisplayAlerts = False
Dim Del As Variant
    Del = Array("4-0", "4-1", "4-2", "4-3", "4-4")
    nn = UBound(Del)
    For i = 1 To nn + 1
        Sheets(Del(i - 1)).Delete
    Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
 
Upvote 0
How about
Code:
For Each Sht In ActiveWorkbook.Worksheets
    If Sht.Visible Then
      If Sht.Name = "4-0" Or Sht.Name = "4-1" Or Sht.Name = "4-2" Then
        Sht.Delete
      End If
    End If
Next Sht

Why are there no parenthesis and why two IF statements?
thanks
 
Last edited:
Upvote 0
Why are there no parenthesis?
There doesn't need to be. Equality (=) has a higher priority than AND and OR.
However, it doesn't hurt to put them in there. I often do, as it makes more sense and makes it easier for me to see what is going on.
 
Upvote 0
Try this:
Code:
Sub Delete_Sheets()
'Modified 3-7-18 4:00 PM EST
Application.ScreenUpdating = False
Dim i As Long
Dim nn As Long
Application.DisplayAlerts = False
Dim Del As Variant
    Del = Array("4-0", "4-1", "4-2", "4-3", "4-4")
    nn = UBound(Del)
    For i = 1 To nn + 1
        Sheets(Del(i - 1)).Delete
    Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

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?
 
Last edited:
Upvote 0
Why are there no parenthesis and why two IF statements?
thanks

Two Ifs?

Because in your original post you said if visible

So first we have to only look at sheets that are visible then look and see what sheet name is.
 
Upvote 0

Forum statistics

Threads
1,223,270
Messages
6,171,102
Members
452,379
Latest member
IainTru

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