Excel crashing when running .Delete

bbalch

Board Regular
Joined
Feb 23, 2015
Messages
61
I'm on Microsoft 365 for business with Excel version 2006 (Build 13001.20266)

Excel is crashing when I run code with .Delete. Any idea how to get around this?

I have some code that deletes sheets not listed in an array. This Example 1 code has worked fine in the past but started crashing excel when I try to run it now. The code below is a simplified version of the Arr list, it would typically be 10 - 15 worksheets. In the example below, assume there are 5 sheets...Sheet 1 - Sheet 5. This code would delete Sheet 2 and Sheet 4.

Example 1
VBA Code:
    Dim Arr As Variant
    Dim Sht As Worksheet
    
    Arr = Array("Sheet1", "Sheet 3", "Sheet 5")
    
Application.DisplayAlerts = False
    For Each Sht In Worksheets
        If Not UBound(Filter(Arr, Sht.Name, True, vbTextCompare)) >= 0 Then Sht.Delete
    Next Sht
Application.DisplayAlerts = True

I tried to delete the individual sheets as a workaround and Excel crashed again.

Code:
Sheets("Sheet 2").Delete
Sheets("Sheet 4").Delete
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
If I delete the sheets manually it works as expected. The sheets are deleted.

The message pops up"Microsoft Excel will permanently delete this sheet. Do you want to continue?" Select Delete and the sheet is deleted.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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