I am using this sub to delete expired sheets within a worksheet by
comparing the date of sheet name with the current date. However,
running this sub always results in Excel crash ! What I mean, the
Excel is frozen and I am asked to quit the Excel completely. I cannot
even go into the VBA again to check the line.
Sub DeleteOld()
For Each Sheet In Worksheets
If IsDate(Sheet.Name) Then
If DateValue(Sheet.Name) <= DateValue(Date - 14) Then
Application.DisplayAlerts = False
Sheet.Delete
End If
End If
Next
End Sub
I have to reopen the file again, but the crash always happens when I
run the Sub again.
So, I tried to change Sheet.Delete to something like
Sheet.Cells.Delete or Sheets.Cells.Clearcontents, the VBA can run
without problem after change. But once I restored to Sheet.Delete,
again the crash problem came out.
I used this kind of Sub in the other Excel file, the problem still
persists.
Could somebody tell me why ?