Note: The other languages of the website are Google-translated. Back to English
English English

How to delete sheet if sheet name not in a list?

Let’s say, I have a worksheet which contains a list of sheet names within the workbook in column A as below screenshot shown, and some certain sheet names in the workbook are not in the list, now, I want to delete the sheets which are not in the list of column A. How could I solve this task in Excel quickly and easily?

doc delete sheet not in list 1

Delete sheets which are not in a specific list with VBA code

arrow blue right bubble Delete sheets which are not in a specific list with VBA code

Normally, you can check the sheet names one by one, and delete those are not in the sheet names list. But, here, I can talk about a VBA code to deal with it. Please do as follows:

1. Go to the worksheet which contains the sheet names list, and then hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.

2. Then click Insert > Module, and paste the following code in the Module Window.

VBA code: Delete sheets which are not in a list

Sub Deletenotinlist()
'Updateby Extendoffice
    Dim i As Long
    Dim cnt As Long
    Dim xWb, actWs As Worksheet
    Set actWs = ThisWorkbook.ActiveSheet
    cnt = 0
    Application.DisplayAlerts = False
    For i = Sheets.Count To 1 Step -1
        If Not ThisWorkbook.Sheets(i) Is actWs Then
            xWb = Application.Match(Sheets(i).Name, actWs.Range("A2:A6"), 0)
            If IsError(xWb) Then
                cnt = cnt + 1
            End If
        End If
    Application.DisplayAlerts = True
    If cnt = 0 Then
        MsgBox "Not find the sheets to be seleted", vbInformation, "Kutools for Excel"
        MsgBox "Have deleted" & cnt & "worksheets"
    End If
End Sub

Note: In the above code, A2:A6 is the cell reference which contains the sheet names, you can change it to your need.

3. And then press F5 key to run this code, the sheets which are not in the list have been deleted at once, see screenshot:

doc delete sheet not in list 2

The Best Office Productivity Tools

Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%

  • Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
  • Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
  • Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
  • Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
  • Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
  • Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
  • Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
  • Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
  • More than 300 powerful features. Supports Office/Excel 2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial. 60-day money back guarantee.
kte tab 201905

Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
officetab bottom
Comments (4)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site

My list of sheet names are numbers only and this code still deletes all the sheets names which are just number i.e I have sheets named "1", "2" and "3" and my list is 1 and 3 only, instead of deleting sheet "2" it deletes all numbered sheet. If the sheet name has any letters then the code works fine. Can someone help?!
This comment was minimized by the moderator on the site
How could this be changed so if the sheet name contains the text in the list it does not delete them rather than if it matches.
This comment was minimized by the moderator on the site

The code deleted all the sheets even those that are included in the list.

Could you please suggest a solution?
This comment was minimized by the moderator on the site
Hello, nelavai
The code works well in my workbook, could you upload a screenshot of your problem or an attachment of your workbook here? So that, we can help to check the problem.
Thank you!
There are no comments posted here yet
Leave your comments
Posting as Guest
Rate this post:
0   Characters
Suggested Locations