Q:

Delete sheet based on a cell value

I want to delete a sheet based on a cell value.
For example, I want cell "T3" to have the value of the sheet I am looking to delete. There are 50+ sheets and all have number values of 4 digits (1234).
Sub Rectangle2_Click()
'deactivate alerts - stop 'are you sure you want to delete pop up'
Application.DisplayAlerts = False

'set worksheet you want to delete
sheettodelete = Range("T3").Value

'deletes sheet
Worksheets(sheettodelete).Delete

're-enable alerts
Application.DisplayAlerts = True
End Sub

The code above is one that I found that is supposed run the macro, however, it errors out at "Worksheets(sheettodelete).delete" with an error Run-tim Error '9': Subscript out of range. I checked and confirmed that the sheet does exist on the workbook.

A:

It executes fine here, however, on a second attempt it will obviously fail since the object won't exist anymore. Also if there is a typo in T3 you will receive this error.
Here's a slight improvement that won't generate the error:
Sub test()
Dim sheetToDelete as String
Dim ws as Worksheet
Application.DisplayAlerts = False

sheetToDelete = Worksheets("SheetThatHoldsTheCell").Range("T3").value

On Error Resume Next 'To avoid the subscript out of range error
Set ws = Worksheets(sheetToDelete)
On Error Goto 0 

If not ws is nothing then ws.delete 'Test if the sheet exists.

Set ws = Nothing

Application.DisplayAlerts = True
End Sub

