'Delete sheet if exists in Excel with VBA

Sub DeleteSheets2()

Application.DisplayAlerts = False

For Each WS In Worksheets
    If WS.Name = "HIT_Qualys_Scan" Then
        Sheets("HIT_Qualys_Scan").Delete
    End If
    
    If WS.Name = "Status" Then
        Sheets("Status").Delete
    End If
    
Next WS

Application.DisplayAlerts = True

End Sub

The above works but I always end up getting an Error that I have to End and then run the Sub again.

I get:

run-time error 424

with line:

If WS.Name = "Status" Then

Once I hit "End" and run it again, it works. Is there a way to avoid this error? I am confused why it runs the 2nd time once I hit End.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source