Macro code Delete Sheet but ask first what sheet name to delete

jeffllacer

New Member
Joined
Aug 21, 2018
Messages
1
Good Day,

Is there a way create a button that will delete a specific sheet but it will prompt a message first and ask what specific sheet name to delete?

Thank you in advance.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Yup. Could use input box function
then loop over the workbook sheets and if the input is found, delete. Confirmation could be via message box function; cancel if response is No, delete if Yes. Obviously you want to disallow deleting the sheet that the button is on? In my limited vba referencing of sheet names I've found that sometimes case matters and Sheet1 is not the same as sheet1 so there's that. Some of that can be avoided by using a listbox that contains sheet names, so no typos. That could also allow you to do 2 or more sheets at a time, instead of just one.

Maybe just right click on the sheet tab and choose delete instead?
 
Upvote 0
Try this:
VBA Code:
Sub Delete_Sheet()
'Modified 9/10/2022  10:37:57 PM  EDT
Application.ScreenUpdating = False
On Error GoTo M
Dim ans As String
ans = InputBox("Enter Sheet Name To Delete")
Sheets(ans).Delete
Exit Sub
M:
MsgBox "You entered:" & ans & vbNewLine & "There is no sheet by this name"
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,618
Messages
6,120,544
Members
448,970
Latest member
kennimack

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top