Delete sheets if they are not included in a list. (Not a list range)

bbalch

Board Regular
Joined
Feb 23, 2015
Messages
53
Hello everyone. I'm trying to delete sheets that are not included in a list. I've found some posts that explain how to delete sheets if they are not included in a list, ie an actual range cells A1:A10. However, I would like to delete the sheets listed within the VBA code not a listed range.

I have a some python code below that I would like to replicate in VBA.

Code:
[COLOR=#303336][FONT=inherit]wb [/FONT][/COLOR][COLOR=#303336][FONT=inherit]=[/FONT][/COLOR][COLOR=#303336][FONT=inherit] openpyxl[/FONT][/COLOR][COLOR=#303336][FONT=inherit].[/FONT][/COLOR][COLOR=#303336][FONT=inherit]load_workbook[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]'testdel.xlsx'[/FONT][/COLOR][COLOR=#303336][FONT=inherit])[/FONT][/COLOR]<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; white-space: inherit;">[COLOR=#303336][FONT=inherit]ws [/FONT][/COLOR][COLOR=#303336][FONT=inherit]=[/FONT][/COLOR][COLOR=#303336][FONT=inherit] wb[/FONT][/COLOR][COLOR=#303336][FONT=inherit].[/FONT][/COLOR][COLOR=#303336][FONT=inherit]get_sheet_names[/FONT][/COLOR][COLOR=#303336][FONT=inherit]()[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
[/FONT][/COLOR][COLOR=#101094][FONT=inherit]for[/FONT][/COLOR][COLOR=#303336][FONT=inherit] sheetName [/FONT][/COLOR][COLOR=#101094][FONT=inherit]in[/FONT][/COLOR][COLOR=#303336][FONT=inherit] ws[/FONT][/COLOR][COLOR=#303336][FONT=inherit]:[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    [/FONT][/COLOR][COLOR=#101094][FONT=inherit]if[/FONT][/COLOR][COLOR=#303336][FONT=inherit] sheetName [/FONT][/COLOR][COLOR=#101094][FONT=inherit]not[/FONT][/COLOR][COLOR=#303336][FONT=inherit] [/FONT][/COLOR][COLOR=#101094][FONT=inherit]in[/FONT][/COLOR][COLOR=#303336][FONT=inherit] [/FONT][/COLOR][COLOR=#303336][FONT=inherit][[/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]'Sheet25'[/FONT][/COLOR][COLOR=#303336][FONT=inherit],[/FONT][/COLOR][COLOR=#303336][FONT=inherit] [/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]'Sheet50'[/FONT][/COLOR][COLOR=#303336][FONT=inherit],[/FONT][/COLOR][COLOR=#303336][FONT=inherit] [/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]'Sheet75'[/FONT][/COLOR][COLOR=#303336][FONT=inherit],[/FONT][/COLOR][COLOR=#303336][FONT=inherit] [/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]'Sheet100'[/FONT][/COLOR][COLOR=#303336][FONT=inherit]]:[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
        sheetToDelete [/FONT][/COLOR][COLOR=#303336][FONT=inherit]=[/FONT][/COLOR][COLOR=#303336][FONT=inherit] wb[/FONT][/COLOR][COLOR=#303336][FONT=inherit].[/FONT][/COLOR][COLOR=#303336][FONT=inherit]get_sheet_by_name[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#303336][FONT=inherit]sheetName[/FONT][/COLOR][COLOR=#303336][FONT=inherit])[/FONT][/COLOR][COLOR=#303336][FONT=inherit] [/FONT][/COLOR][COLOR=#303336][FONT=inherit]//[/FONT][/COLOR][COLOR=#303336][FONT=inherit] gets the sheet object
        wb[/FONT][/COLOR][COLOR=#303336][FONT=inherit].[/FONT][/COLOR][COLOR=#303336][FONT=inherit]remove_sheet[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#303336][FONT=inherit]sheetToDelete[/FONT][/COLOR][COLOR=#303336][FONT=inherit])[/FONT][/COLOR][COLOR=#303336][FONT=inherit] 
wb.save('testdel2.xlsx')[/FONT][/COLOR]</code>
Any ideas or suggestions on how to accomplish this using VBA?

Thanks in advance for your help.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,151
Office Version
365
Platform
Windows
This should delete the sheets
Code:
Sub DelSheets()

    Dim Arr As Variant
    Dim Sht As Variant
    
    Arr = Array("Sheet1", "Sheet2", "Sheet3")
    
Application.DisplayAlerts = False
    For Each Sht In Arr
        Sheets(Sht).Delete
    Next Sht
Application.DisplayAlerts = True

End Sub
 

bbalch

Board Regular
Joined
Feb 23, 2015
Messages
53
This does delete the sheets listed. However, I'm trying to delete all the sheets that are not listed. For example, if I have sheet1 - sheet100 and only need to keep Sheet25, Sheet50, Sheet75, and Sheet100 I would like to add those to the VBA list and delete all other tabs.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,151
Office Version
365
Platform
Windows
I saw this part of your op
However, I would like to delete the sheets listed within the VBA code not a listed range.
& ignored the previous part :oops:
Try this instead
Code:
Sub DelSheets()

    Dim Arr As Variant
    Dim Sht As Worksheet
    
    Arr = Array("Sheet1", "Sheet2", "Sheet3")
    
Application.DisplayAlerts = False
    For Each Sht In Worksheets
        If Not UBound(Filter(Arr, Sht.Name, True, vbTextCompare)) >= 0 Then Sht.Delete
    Next Sht
Application.DisplayAlerts = True

End Sub
 

bbalch

Board Regular
Joined
Feb 23, 2015
Messages
53
That did the trick! Thank you very much!
 

Truiz

Active Member
Joined
Jul 14, 2014
Messages
332
Here is an alternative.

Code:
Sub DSITANIIAL()
Dim Sh As Worksheet
     
 With Application
 .ScreenUpdating = False
 .DisplayAlerts = False
 End With
    For Each Sh In ActiveWorkbook.Worksheets
          Select Case Sh.Name
               Case "[COLOR=#ff0000]Sheet1[/COLOR]", "[COLOR=#ff0000]Sheet2[/COLOR]", "[COLOR=#ff0000]Sheet3[/COLOR]"
               [COLOR=#009900]'If it matches it won't do anything[/COLOR]
               Case Else
               Sh.Delete
           End Select
   Next Sh
 With Application
 .ScreenUpdating = True
 .DisplayAlerts = True
 End With
 End Sub
 
Last edited:

Forum statistics

Threads
1,081,863
Messages
5,361,743
Members
400,654
Latest member
Pinaki Chatterjee

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top