VBA skips deleting a sheet

Guzzlr

Well-known Member
Joined
Apr 20, 2009
Messages
977
Office Version
  1. 2021
Platform
  1. Windows
Hello
I have:

VBA Code:
Sub ClearContents()
Application.ScreenUpdating = True
Application.DisplayAlerts = False

'********* Delete Sheets **********
    Sheets("NexGen Report Paste").Select
    ActiveWindow.SelectedSheets.Delete
   
    Application.Wait (Now + TimeValue("00:00:03"))
   
    Sheets("Tech Description Report").Select
    ActiveWindow.SelectedSheets.Delete


'Return to Template Tab
Sheets.Add.Name = "Tech Description Report"
Sheets.Add.Name = "NexGen Report Paste"
Sheets("NexGen Report Paste").Select
Range("A1").Interior.ColorIndex = 4
Range("A1").Select

Application.ScreenUpdating = False
Application.DisplayAlerts = True

End Sub

So I am trying to delete the two sheets above.
If I manually run the code with F8, the sheets delete.
However, when I install a button with sub ClearContents, the sheet of "Tech Description Report" is skipped...as if it is moving too fast.
So, I added a wait of 3 seconds to hope that it will "catch up" but it still skips over the sheet of "Tech Description Report". But again, if I manually move through the code, the sheet does delete.
Maybe instead of having two separate line areas for deleting...combine into one?

I'm curious why this is happening.

Thanks for the help
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try replacing your delete section with this code:
VBA Code:
'********* Delete Sheets **********
    Dim ws As Worksheet
    Application.DisplayAlerts = False
    For Each ws In ActiveWorkbook.Worksheets
        If (ws.Name = "NexGen Report Paste") Or (ws.Name = "Tech Description Report") Then ws.Delete
    Next ws
    Application.DisplayAlerts = True
 
Upvote 0
Solution
Try replacing your delete section with this code:
VBA Code:
'********* Delete Sheets **********
    Dim ws As Worksheet
    Application.DisplayAlerts = False
    For Each ws In ActiveWorkbook.Worksheets
        If (ws.Name = "NexGen Report Paste") Or (ws.Name = "Tech Description Report") Then ws.Delete
    Next ws
    Application.DisplayAlerts = True

Working!
Thank you
 
Upvote 0
You are welcome.
 
Upvote 0

Forum statistics

Threads
1,216,081
Messages
6,128,694
Members
449,464
Latest member
againofsoul

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