In this video, we're going to see how we can delete a sheet using VBA. So you could use the worksheet or the sheet delete method. So if you write worksheets, sheet two dot delete, so this is going to actually delete sheet two. But if you run that, actually Excel will prompt you whether you would like to continue with deleting. So if you press the delete button, the sheet is going to be deleted. Or if you press cancel, the sheet will not be deleted.
And this is just as when you do it in the normal Excel interface when you right click and click on Delete for a sheet, you got this prompt. Okay. So I'm going to show you how you can suppress this prompt and not have it appear and have the sheet deleted straight away. So you could write application dot display alerts equals false and this will actually suppress any other alerts that would appear on the screen any prompts that will be suppressed starting from this line of code. And you could follow that with application dot display alerts equals true at any point that you would like these alerts are these prompts to be displayed again. So this line of code will make prompts get displayed again, as usual.
And you know, of course, that the code the VBA code gets executed from top to bottom. So we're going to disable the prompts, delete the sheet, with the prompts being disabled, so we won't have the problems and then enable them again. And this is what you normally do when you want to disable a feature a normal feature in Excel, you just disable it temporarily, and then you enable it again. And this is useful when debugging your code. And we'll speak about debugging later on in the course. But this is useful because This makes you have any problems or problems with your code displayed on the screen when you just re enable these alerts again.
So if you run this code, you can see here that she too has been deleted, and we haven't had the prompt displayed. One more thing I want to show you as well is if we try to delete a sheet that is actually the last remaining sheet in our workbook. So if you try to do that in the normal Excel interface, if you right click on this sheet and this sheet one is the last one that is remaining, it will prompt you this will close the workbook without saving some micro modules will be lost. Do you want to move the selected cheats anyway, but if I click on yes here, actually, it says a workbook must contain at least one visible worksheet and to hide delete or move the selected sheets, you must first insert a new sheet or unhide a sheet that is already hidden. And if I press OK, nothing has been done and actually A workbook needs to contain at least one sheet or worksheet.
So actually you cannot delete a sheet it is the last remaining worksheet in the workbook. Okay. So, if you run this code, you will get an error you cannot delete the sheet that is the last remaining worksheet or sheet in the workbook. Okay, so that's it guys for deleting sheets. Thank you very much for watching this video. I'll see you on the next one.