To delete a sheet using VBA, you need to use the VBA Delete method. You need to specify the sheet that you want to delete and then use this method. Let’s say if you want to delete the “Sheet1”, then you need to mention sheet1 and then type a dot (.) and in the end, type “Delete”.
In this tutorial, we will see different ways that you can use to delete a sheet using a VBA code. Make sure to have the developer tab on the ribbon from here you can get into the visual basic editor.
Delete a Sheet using its Name
Each sheet has a name, and you can use write a code to delete a sheet using the name. So, let’s say you want to delete the worksheet “Data”, the code would be:
Sub vba_delete_sheet()
Sheets("Data").Delete
End Sub
Delete a Sheet without Warning (Prompt)
When you delete a worksheet, Excel shows a message to confirm if you want to remove it or wish to cancel. And when you use a VBA code, in that case, Excel will also do that.
To deal with this, you can turn OFF the screen updating to delete a sheet and then turn it ON.
Application.DisplayAlerts = False
Sheets("Data").Delete
Application.DisplayAlerts = True
Name of the Sheet from a Cell
Now let’s say you want to use a cell value to use the name of the worksheet. In that case, you need to use the VBA range object to do that.
Delete the Sheet using the Sheet Number
That’s right. You can use the sheet’s number to delete it. Here’s the code.
Sub vba_delete_sheet()
Sheets(1).Delete
End Sub
Delete the ActiveSheet
To delete the active sheet, you can use the “ActiveSheet” object instead of using the sheet name to specify the sheet.
ActiveSheet.Delete
As I said, it deletes the active sheet, and you can activate it before removing it. But necessarily, you don’t need to do that as you can refer to a sheet and delete it as we have seen at the start of this tutorial.
Check IF the Sheet Exists Before Deleting
You can also write code in a way that it can check if the sheet exists or not and then deletes it.
Sub check_sheet_delete()
Dim ws As Worksheet
Dim mySheet As Variant
mySheet = InputBox("enter sheet name")
Application.DisplayAlerts = False
For Each ws In ThisWorkbook.Worksheets
If mySheet = ws.Name Then
ws.Delete
End If
Next ws
Application.DisplayAlerts = True
End Sub
In this code, you have FOR EACH to loop through all the worksheets. And then, an IF STATEMENT to delete the sheet if its name is equal to the name you have typed in the input box.
Delete All the Worksheets in Workbook
I’m sure you have this question in your mind, but I’m afraid it’s impossible to delete all the worksheets that you have in the workbook. You must have at least one worksheet left. But I have found a solution to this problem.
You can insert a new sheet that’s a blank one and then delete all which are already there.
Here’s the code: This code adds a new sheet and deletes all the other sheets.
Sub vba_delete_all_worksheets()
Dim ws As Worksheet
Dim mySheet As String
mySheet = "BlankSheet-" & Format(Now, "SS")
Sheets.Add.Name = mySheet
Application.DisplayAlerts = False
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> mySheet Then
ws.Delete
End If
Next ws
Application.DisplayAlerts = True
End Sub
[helpful]