Excel Guides

Deleting Worksheets in a Macro in Excel

Deleting worksheets can be a very tedious task, especially if you have a lot of them. Luckily, there is a way to automate this process using macros in Excel. This tutorial will show you how to delete worksheets in a macro in Excel.

First, open the workbook that contains the worksheets you want to delete. Then, press Alt+F11 to open the Visual Basic Editor. In the editor, double-click on ThisWorkbook in the Project Explorer window.

Paste the following code into the ThisWorkbook code window:

Private Sub Workbook_Open()

  Dim ws As Worksheet

  For Each ws In ThisWorkbook.Worksheets

    If ws.Name <> "Sheet1" Then

      ws.Delete

    End If

  Next ws

End Sub

This code will delete all worksheets except for Sheet1. If you want to delete Sheet1 as well, then you can remove the line that says If ws.Name <> "Sheet1" Then. Once you've made your changes, close the Visual Basic Editor and save your workbook.

Now, every time you open your workbook, all of the worksheets will be deleted automatically!

Move beyond 

Excel

Get started with Causal today.
Build models effortlessly, connect them directly to your data, and share them with interactive dashboards and beautiful visuals.