I'm using Microsoft 365 for Business - Excel version 2107
I have a workbook with multiple tabs and have the code below that deletes worksheets that are not listed in the array. I run this code on a monthly basis to generate reports, and the code has worked without any problems in the past.
I'm trying to run the code now and it causes Excel to crash. It will enter the For Loop and delete 2 or 3 sheets and then it crashes. Any ideas, on what is causing the problem?
The day, filepath, and f_name are global variables outside of this sub.
I have a workbook with multiple tabs and have the code below that deletes worksheets that are not listed in the array. I run this code on a monthly basis to generate reports, and the code has worked without any problems in the past.
I'm trying to run the code now and it causes Excel to crash. It will enter the For Loop and delete 2 or 3 sheets and then it crashes. Any ideas, on what is causing the problem?
The day, filepath, and f_name are global variables outside of this sub.
VBA Code:
Option Explicit
Public day As String
Public filepath As String
Public f_name As String
Sub run()
Dim wb As Workbook
Dim myfilename As String
Dim Sht As Worksheet
Dim Arr As Variant
Dim wsName As Variant
Dim Matched As Boolean
myfilename = filepath & f_name & "Shared Other - " & day & ".xlsx"
' open the workbook and pass it to workbook object variable
Set wb = Workbooks.Open(myfilename)
Application.ScreenUpdating = False
' Delete Tabs that are not listed
Arr = Array("22-420 Other Special Acti", "22-425 Touring Awards", "22-430 Triple Play Awards", "22-440 Board")
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
' Save and Close Workbook
wb.Save
wb.Close
End Sub