Skip to main content

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

Author: Xiaoyang Last Modified: 2020-08-07

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

Best Office Productivity Tools

🤖 Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functions
Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |   Round without Formula ...
Super Lookup: Multiple Criteria VLookup    Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup ....
Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List ....
Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ...
Featured Features: Grid Focus   |  Design View   |   Big Formula Bar    Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells    Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ...
Top 15 Toolsets12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more

Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time.  Click Here to Get The Feature You Need The Most...


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!
Comments (4)
No ratings yet. Be the first to rate!
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!
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

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?!
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
Rate this post:
0   Characters
Suggested Locations