Delete a specific sheet depending on a value of a cell

Kayslover

Board Regular
Joined
Sep 22, 2020
Messages
168
Office Version
  1. 2013
Platform
  1. Windows
Hi,

I would like to delete sheet named Sheet5 if the value of H2 in sheet named Formula is 4.

I intend to use this in a macro I use.

Thanks in anticipation
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Hello!
VBA Code:
Sub Kayslover()
Dim sh As Worksheet
'Application.DisplayAlerts = 0 'uncomment these commented lines to delete sheet without confirmation
    On Error Resume Next
    
    For Each sh In Worksheets
        If ThisWorkbook.Sheets("Formula").Range("H2") = 4 Then Sheets("Sheet5").Delete
    Next sh
'Application.DisplayAlerts = 1
End Sub
 
Upvote 0
@LazyBug
There is no need/reason to cycle through all the worksheets. You loop does not use the variable sh within it and if there happened to be 50 sheets and
- 'Formula' H2 was 4, the loop would be deleting or trying to delete the same worksheet 50 times
- 'Formula' H2 was not 4 the loop would still check that same cell 50 times and each time decide no deletion was needed.

My suggestion
VBA Code:
Sub CheckAndDelete()
  If Sheets("Formula").Range("H2").Value = 4 Then
    Application.DisplayAlerts = False
    On Error Resume Next
    Sheets("Sheet5").Delete
    On Error GoTo 0
    Application.DisplayAlerts = True
  End If
End Sub
 
Upvote 0
@Peter_SSs, thank you, I'm very appreciate your explanation!
I admire your skills so much and I'm glad to learn from such masters at the forum.
 
Upvote 0
Lazyboy, Peter_SSs,

Thank you both for your solutions. I am a novice, and have a question.

The fact we know that we only want to delete sheet called Sheet5, why do we have to have a loop to cycle throught the whole workbook?

Another question I have, having delete the sheet5, can I have a message that says "5th Sheet deleted" and if sheet5 has not been deleted, then a message "5th Sheet does not require deleting".

Thanks in anticipation.
 
Last edited:
Upvote 0
Peter_SSs,

Thanks for the prompt response and explaination.

What about:-

Having deleted the sheet5, can I have a message that says "5th Sheet deleted" and if sheet5 has not been deleted, then a message "5th Sheet does not require deleting".
 
Upvote 0
Is this what you mean?

VBA Code:
Sub CheckAndDelete()
  If Sheets("Formula").Range("H2").Value = 4 Then
    Application.DisplayAlerts = False
    On Error Resume Next
    Sheets("Sheet5").Delete
    On Error GoTo 0
    Application.DisplayAlerts = True
    MsgBox "Sheet5 deleted"
  Else
    MsgBox "Sheet5 not deleted"
  End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,608
Messages
6,120,500
Members
448,968
Latest member
screechyboy79

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