There are times when we have to create or add sheet and later on we found of no use of that sheet, hence we get need to delete sheet quickly from the workbook. This article focus on saving time & provide code for removing sheets without any prompt message box using vba macro / code. It could be one sheet or several. And excel gives a popup alert to confirm the sheet deletion. If its one sheet, its manageable. But if you have to delete several sheets, you will get one popup per sheet.
You often create multiple sheets using VBA. But in the end, you don’t need them actually. You would want to delete them after your work is done. It will keep your file lighter, faster and sensible.
VBA code to delete a sheet is simple
Let's say you have this code to add a sheet and then delete it when work is done.
Sub AddAndDeleteSheet() Sheets.Add 'Adds a new sheet to the active workbook '---- '---- '---- 'some work on sheet done ActiveSheet.Delete 'deletes the active Activesheet End Sub
When you execute this code, Sheets.Add will work without any prompt but when compiler will come to ActiveSheet.Delete it will prompt a message like this.
Since you are deleting sheets using VBA, you know what you are doing. You would like to tell Excel not to show this warning and delete the damn sheet.
To do this we will switch off the display alert button of Excel Application.
The code below will bypass this alert message and the sheet will be deleted without any intimation.
Sub AddAndDeleteSheet() Application.DisplayAlerts = False 'switching off the alert button Sheets.Add '---- '---- '---- 'some work on sheet done ActiveSheet.Delete Application.DisplayAlerts = True 'switching on the alert button End Sub
Here we are deleting Activesheet using VBA. You can delete any sheet using VBA. Just write Sheets("Sheetname").delete. Moving on...
DisplayAlerts is a property of Application object in VBA. Here we are switching it off at the beginning of our code and switching it on at the end of the code. The above code will confirm deletion without any intimation.
Note:The above code will ignore all the warnings thrown by excel. If you want to allow only sheet deletion than use this line.
Application.DisplayAlerts = False 'switching off the alert button ActiveSheet.Delete Application.DisplayAlerts = True 'switching on the alert button
This will ignore only sheet deletion warning.
If you have multiple sheets to delete and are using a loop, try this code –
Option Explicit Sub macro2() Dim i As Long Application.DisplayAlerts = False For i = 1 to Worksheets.Count If Worksheets(i).Name Like "Test*" Then Worksheets(i).Delete Next i Application.DisplayAlerts = True End Sub
That’s it. It’s done. It easy like switching your fan on and off. Isn’t it?
If you liked our blogs, share it with your friends on Facebook. And also you can follow us on Twitter and Facebook.
We would love to hear from you, do let us know how we can improve, complement or innovate our work and make it better for you. Write us at email@example.com
Delete sheets without confirmation prompts using VBA in Microsoft Excel
The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.