Delete sheet

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am wanting to enter the name of a worksheet in D1, and have a macro that
looks for the worksheet with that name and automatically delete it without
prompts or warnings. Can someone help me out with that code?

Thanks so much
 
right click sheet tab>view code>insert this. If you make a mistake, don't
SAVE the workbook.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> "$D$1" Then Exit Sub
Application.DisplayAlerts = False
Sheets(Target.Value).Delete
Application.DisplayAlerts = True
End Sub
 
Thanks Don!

Don Guillett said:
right click sheet tab>view code>insert this. If you make a mistake, don't
SAVE the workbook.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> "$D$1" Then Exit Sub
Application.DisplayAlerts = False
Sheets(Target.Value).Delete
Application.DisplayAlerts = True
End Sub
 
Same as Dons but with code for errors
Private Sub Worksheet_Change(ByVal Target As Range)
Dim strmsg As String
On Error GoTo SheetError
If Target.Address <> "$D$1" Then Exit Sub
Application.DisplayAlerts = False
Sheets(Target.Value).Delete
Application.DisplayAlerts = True
SheetError:
strmsg = "Error # " & Err & " : " & Error(Err)
Select Case Err

Case 9:

MsgBox strmsg & vbCrLf & "Sheet you selected is not a valid sheet"
Exit Sub

Case Else:

MsgBox strmsg
Exit Sub
End Select
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top